【发布时间】:2018-01-31 08:32:04
【问题描述】:
这是代码:
var http = require('http')
var options = {
hostname: 'localhost',
method: 'POST',
port: 8000,
path: '/'
}
var s = 3;
http.request(options, (res)=>{
}).end(s+'')
http.createServer((req, res)=>{
res.writeHead(200, {'Content-type': 'text/plain'})
var a = "";
req.on('data', (data)=>{
a+= data
})
req.on('end', ()=>{
res.write(a)
res.end()
})
}).listen(8000)
为什么当预期返回值为 3 时,服务器可能会向客户端返回无效信息?
【问题讨论】:
-
你确定 res.write() 当前返回了什么?
-
目前不使用 res.write() 返回任何内容
-
好的。我建议看看:nodejs.org/api/http.html#http_http_request_options_callback。也许你已经有了。无论如何,他们在该页面上有一个示例实现。
-
如果您正在尝试实现节点 HTTP 服务器,请查看 express。它抽象了很多使用节点
httpapi 的细节。 -
我需要它在不使用 express 的情况下工作
标签: javascript node.js node.js-stream node.js-connect node.js-client