【问题标题】:Expressjs on a cPanel environmentcPanel 环境中的 Expressjs
【发布时间】:2022-11-24 09:43:06
【问题描述】:

我正在尝试使用 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 尝试相同的示例时,出现错误 Cannot get /node/index.php

我的 Express 应用程序的 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 Internal Server Error 错误。

有任何想法吗?

【问题讨论】:

    标签: node.js express channel


    【解决方案1】:

    问题是基本的 cPanel 计划由于某种原因不支持 node.js 应用程序(还没有深入挖掘以支持原因)。

    对我有用的是获取 CloudLinux 附加组件并通过提供的“设置 Node.js 应用程序”从 software 选项卡运行我的应用程序。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2018-12-07
      • 2015-01-09
      相关资源
      最近更新 更多