【问题标题】:nodejs, Express Cannot GET /nodejs, Express 无法获取 /
【发布时间】:2018-07-01 12:26:11
【问题描述】:

我尝试在云服务器上运行最简单的 Express 代码

var express = require('express');
var app = express();
app.get('/test', (req, res) => res.send('Hello ')); 
app.listen( 3000 );  

我收到 Apache 404 错误:GET /cood180119/projExpress1/demo1/test HTTP/1.0,无法获取 /cood180119/projExpress1/demo1/test

我注意到我的网址是http://77.xx.xx.xx/cood180119/projExpress1/demo1/test

虽然错误日志显示 IP : 88.xx.xx.xx。

如何解决?

我有一个 node hello world 示例,它使用以下 url:

http://77.xx.xx.xx/node-hello-world-master/

文件在public_html/node-hello-world-master/app.js中的位置,

但不适用于http://77.xx.xx.xx:3000/

const http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end("Hello, World!\n  port= ' + process.env.PORT);
}).listen(process.env.PORT); //is says that port is undefined

我在环境节点变量中设置了端口=3000。 在此之后,我可以得到端口号回显,但我仍然无法使用 ttp://77.xx.xx.xx:3000/ 访问网页

似乎防火墙配置错误。

【问题讨论】:

    标签: node.js apache express http-status-code-404


    【解决方案1】:

    我认为您请求的网址有误。

    app.get('/test', (req, res) => res.send('Hello ')); app.listen( 3000 );

    这将始终产生如下路线 'http://ip:3000/test

    你为什么在你的 URL 中使用 cood180119/projExpress1/demo1。我没有看到为此定义的任何路线。

    【讨论】:

    • 这是通过向 app.js 显示文件夹结构以其他方式形成 url 的方式。我已经更新了这个问题,因为它仍然不起作用,似乎原因是端口。如何获取端口号?
    • 我有一个没有 express 的节点应用程序,如果 url 是文件夹结构类型,它可以工作,但如果我放端口则不能工作。似乎我提供了错误的端口号。主机获取端口号?
    • 云服务器是什么意思? AWS 或其他云提供商?
    【解决方案2】:

    你的路线

    app.get('/test', (req, res) => res.send('Hello '));

    确保您要捕获的请求的URI(或本例中的路径/test)存在。

    【讨论】:

      【解决方案3】:

      我的问题:

      html 文件的名称不完全是“index.html”

      所以,我重命名了它,它运行良好!

      app.use(express.static('public'));
      

      注意:public 是我的服务器目录中的一个文件夹,其中包含 index.html 和 styles.css。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-31
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 2018-06-17
        相关资源
        最近更新 更多