Expressjs on a cPanel environment



我正在尝试用expressjs构建一个API。虽然我能够运行一个非常简单的测试与基本的http,如下面的代码

const http = require('http')
const hostname = '127.0.0.1';
const port = 3003;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World! I am your new NodeJS app! n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

当我尝试与expressjs相同的例子时,我得到一个错误无法获得/node/index.php

我的快递应用程序的app.js代码在

const express = require('express');
const app = express();
const port = 3003;
app.get('/', (req, res) => {
res.status(200).send("Hello");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});

当我进入终端并点击node app.js时,我得到控制台日志部分,但页面无法加载。

我还认为值得一提的是,我的.htaccess看起来像这样

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:3003 / [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3003 /$1 [P,L]

我尝试在我的.htaccess中添加DirectoryIndex disabled,但随后我得到500内部服务器错误错误。

任何想法?

问题是基本的cPanel计划不支持node.js应用程序的一些原因(还没有深入挖掘到理解为什么)。

对我有用的是获得CloudLinux插件并通过提供的"Setup Node.js app"从软件选项卡运行我的应用程序。

希望这能帮助像我这样的人,那些试图使Express.js在cPanel上工作的人。

最新更新