【问题标题】:Localhost didn’t send any data. ERR_EMPTY_RESPONSE , Nodejs error本地主机没有发送任何数据。 ERR_EMPTY_RESPONSE , Nodejs 错误
【发布时间】:2020-06-05 19:59:40
【问题描述】:

我刚开始学习 Node.js,但遇到了服务器问题。我尝试了各种解决方案,但都不起作用(更改端口、更新 node.js、更新 chrome 等),但没有任何效果。

服务器打开,但本地主机没有响应。

这是我的 index.js 文件

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  console.log("test");
  res.send("Hello world");
});

app.listen(port, () => {
  console.log(`Server open port ${port}`);
});

这是我的 package.json 文件

  "name": "last-server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

终端视图

$ node app.js
Server open port 3000
test
test
test
test

**服务器启动,但在 Chrome 中没有得到 localhost 的响应。不过,相同的代码在我朋友的设备中也可以使用。 **

【问题讨论】:

    标签: javascript node.js express server


    【解决方案1】:

    你很接近。你导入模块了吗?

    const express = require('express');
    const port = 3003;
    const app = express();
    
    app.get("/", (req, res) => {
        console.log("test");
        res.send("Hello world");
    });
    
    app.listen(port, () => {
        console.log(`Server open port ${port}`);
    });
    

    【讨论】:

    • 注意:我更改了端口,因为我在其他节点应用程序中经常使用端口 3000
    • 您不应该在 cmets 中补充您的答案。请通过编辑改进它。
    • 你很亲密??那是什么意思??是的,我将 npm 包 Express 都导入了,但仍然没有得到任何响应
    • @AkhilRana,我只将第一行添加到你所拥有的内容中,因此,我说你很接近。您安装了该软件包,但这与导入 expess 模块以供节点使用不同。 nodejs.org/docs/latest-v12.x/api/modules.html#modules_modules
    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2020-12-17
    • 1970-01-01
    • 2021-08-20
    • 2018-04-13
    相关资源
    最近更新 更多