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

发布于 2025-01-11 22:32:17 字数 412 浏览 3 评论 0原文

我在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");
});

I write following in nodejs and run it. but can not connect to localhost:8080. I am use http and express module.

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);

or

app.listen(PORT);
app.use('/',(req,res)=>{
    res.send("Hellow world");
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2025-01-18 22:32:17

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

可能是这样:

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

您能放一张确切错误或屏幕的图片吗?重新看到?

When I run it on my pc it works fine.

It could be that:

  • You are not running it from your pc, so it's not running on localhost.
  • The link is not the correct one. The correct one is http://localhost:8080
  • Try adding "localhost" beside PORT to change it to:
http.createServer((req,res)=>{
    res.end('Hellow world')
}).listen(PORT, "localhost");

Could you put a picture of the exact error or screen you're seeing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文