实际上你可以做到这一点。我已经成功地设置了几种不同的方式。我认为第二个可能是你想要的:
1. cgi节点 http://www.cgi-node.org/home
基本上这取代了灯栈上的 PHP。您可以像运行 PHP 一样通过节点运行 javascript。这与 node js 具有所有相同的功能,但仅适用于模板渲染。
<html>
<body>
<?
var helloWorld = 'Hello World!';
write(helloWorld + '<br/>');
?>
<?= helloWorld ?>
<br/>
<b>I can count to 10: </b>
<?
for (var index= 0; index <= 10; index++) write(index + ' ');
?>
<br/>
<b>Or even this: </b>
<?
for (var index= 0; index <= 10; index++) {
?>
<?= index ?>
<? } ?>
</body>
</html>
或
2。独立服务器(适用于 NameCheap 主机和 GoDaddy 共享主机)
在您的共享主机帐户中,您需要 SSH 才能执行此操作。因此,您可能需要升级或向他们的客户支持请求 SSH 访问权限。下载最新的 NodeJS https://nodejs.org/en/download/。共享主机可能是 linux 64 位。您可以通过运行在 linux 或 unix 上进行检查:
uname -a
下载 Linux 二进制文件并将下载的 bin/node(以及 bin/npm 文件,如果您想在服务器上使用 npm)文件放在 /home/username/bin/ 中(如果没有,请创建 bin 文件夹'不存在)在服务器上。将权限 755 放在节点二进制文件上。所以你应该在这里有一个新文件:
/home/username/bin/node
打开 /home/username/public_html 中的 .htaccess 文件并添加以下行:
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
在 /home/username/public_html 中创建一个文件,并将其命名为 app.js。在该文件中添加以下行:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
SSH 进入服务器运行这些命令:
cd /home/username/public_html
which node # this should return ~/bin/node
node app.js & # This will create a background process with the server running
如果您可以正确设置此设置,从长远来看,这将为您节省大量资金,而不是使用 AWS 或 Heroku 等。