使用 Webstorm IDE 的 docker 上的 http 节点服务器出现 ERR_EMPTY_RESPONSE

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

我尝试使用 docker 设置我的 webstorm IDE,以便通过 docker 节点映像运行节点程序。 URL 似乎没问题,但有一个空响应...

这是我的代码。这有什么问题吗

import {createServer} from 'node:http'

const server = createServer((req, res) => {
    res.statusCode(200)
    res.setHeader('Content-Type', 'plain/text')
    res.end('hello world')
}
)

const host='127.0.0.1'
const port=3000
server.listen(port,host,()=>{
    console.log(`Listening on ${host}:${port}`)
})

http 请求给出此错误:

GET http://127.0.0.1:3000

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

谢谢。

node.js docker http webstorm
1个回答
0
投票

替换原始代码中的这些行,这可以修复语法错误并确保您的

Node.js
服务器中正确的响应标头设置。

res.statusCode(200)
res.setHeader('Content-Type', 'plain/text')
© www.soinside.com 2019 - 2024. All rights reserved.