我在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");
});
当我在我的电脑上运行它时,它工作正常。
可能是这样的:
http://localhost:8080
"localhost"
将其更改为:http.createServer((req,res)=>{
res.end('Hellow world')
}).listen(PORT, "localhost");
您能否提供您所看到的确切错误或屏幕的图片?
我遇到了类似的问题,在我的终端中显示了此错误: 节点:内部/模块/cjs/loader:1080 抛出错误; ^ 错误:找不到模块“express”
我搜索了很多,找到了一个网站提供了解决方案...... 在终端中写下这个命令: npm 安装 Express 然后我再次运行代码,它成功了!你也应该尝试一下也许会有效