javascript - Node.js 本地主机 :3000 refuses to connect

标签 javascript node.js windows localhost

我是 Node.js 的初学者,无法连接到 localhost:3000

我在VS代码中使用以下代码,在终端中点击“node app.js”,此时终端中没有出现错误。 但是,当我尝试访问 localhost:3000 时,它一直拒绝:“ERR_CONNECTION_REFUSED” 我在互联网上搜索解决方案,并尝试通过在安全设置上创建入站规则来打开端口,打开 IIS,改用 127.0.0.1,但仍然被拒绝。有谁知道如何解决这个问题吗?

我使用的是 Windows 10


const http = require('http');

var server = http.createServer(
    (request, response)=>{
        response.end('hello');
    }
);

server.listen(3000);

最佳答案

以下是解决方法。您可能尝试在已使用的端口上启动服务器。

// enter this command in your terminal
lsof -i:3000

// this will output the related PID (process ID). Here: 1382.
node      1382 name   21u  IPv6 blabla      0t0  TCP *:3000 (LISTEN)

// kill the PID in use
kill -9 1382

//relaunch your server
node app.js

关于javascript - Node.js 本地主机 :3000 refuses to connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68419931/

相关文章:

windows - 在 Windows 8 中将 Web 应用程序显示为全屏应用程序

windows - 使用VB脚本监控目录

javascript - 如何在 app.locals 函数中使用带有express的 Node 返回值

javascript - webpack 的 import() 什么时候不会创建新文件?

javascript - 在 React 中使用 immutability-helper 设置可变对象键

node.js - 如何高效地创建不同 UUID 的大列表?

javascript - 在 Bootstrap Adminlte 模板的菜单中创建子项时不会自动折叠

javascript - react 组件库的 cjs 或 esm

node.js - Google Cloud Run 上的 Stackdriver-trace 失败,但在本地主机上工作正常

c# - 如何在 C# 中创建临时文件(用于写入)?