无法使用http和express模块连接到nodejs中的localhost

问题描述 投票:0回答:2

我在nodejs中编写以下内容并运行它。但无法连接到 localhost:8080。我正在使用http和express模块。

const http=require('http');
const PORT=process.env.PORT || 8080;
const express=require('express');
const app=express();

http.createServer((req,res)=>{
    res.end('Hellow world')
}).listen(PORT);

app.listen(PORT);
app.use('/',(req,res)=>{
    res.send("Hellow world");
});
node.js express http localhost port
2个回答
0
投票

当我在我的电脑上运行它时,它工作正常。

可能是这样的:

  • 您没有从您的电脑上运行它,因此它没有在本地主机上运行。
  • 该链接不正确。正确的是
    http://localhost:8080
  • 尝试在 PORT 旁边添加
    "localhost"
    将其更改为:
http.createServer((req,res)=>{
    res.end('Hellow world')
}).listen(PORT, "localhost");

您能否提供您所看到的确切错误或屏幕的图片?


0
投票

我遇到了类似的问题,在我的终端中显示了此错误: 节点:内部/模块/cjs/loader:1080 抛出错误; ^ 错误:找不到模块“express”

我搜索了很多,找到了一个网站提供了解决方案...... 在终端中写下这个命令: npm 安装 Express 然后我再次运行代码,它成功了!你也应该尝试一下也许会有效

© www.soinside.com 2019 - 2024. All rights reserved.