gpt4 book ai didi

node.js - Node index.js 命令在 Windows powershell 中被忽略

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:39 25 4
gpt4 key购买 nike

我正在尝试在本地主机中运行index.js。所以,我像这样制作index.js。

const http = require('http');

http.createServer((req, res) => {
res.end('Hello');
}).listen(3000, ()=> {
console.log('running');
})

我输入了 node index.js 但控制台中没有任何内容,它只是自行关闭。

控制台上没有任何内容。

enter image description here

像这样。

我在 PowerShell 和 git bash 中输入了该内容,但它们都不起作用。

我尝试了另一个端口,例如 8080 或 8000,但它不起作用。我还使用了 foreverpm2nodemon 但它也不起作用。

我认为这类似于防火墙问题。但我不知道出了什么问题。

最佳答案

尝试破坏你的代码。

这可能是因为您的代码不保存服务器的实例,它会自行清除......因此甚至没有达到监听 promise 。

const http = require('http');

const server = http.createServer((req, res) => {
res.end('Hello');
})

server.listen(3000, ()=> {
console.log('running');
})

关于node.js - Node index.js 命令在 Windows powershell 中被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56122562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com