求解答~《node.js开发指南》中的例子。
发布于 10 年前 作者 xishvai 4977 次浏览 最后一次编辑是 8 年前

浏览器端代码:post.html

<form method=“post” action=“http://localhost:3000/”> <input type=“text” name=“title” /> <textarea name=“text”></textarea> <input type=“submit” /> </form>

服务器端代码:post-server.js

var http = require(‘http’), util = require(‘util’), querystring = require(‘querystring’);

var server = http.createServer(function(req, res) { var post = ‘’;

req.on('data', function(chunk) {
	post += chunk;
});

req.on('end', function() {
	//console.log(util.inspect(post));
	post = querystring.parse(post);
	res.write(post.title);
	res.write(post.text);
	res.end();
});

}).listen(3000);

服务器端的输出为: ‘title=hw&text=helloworld’ ’’

_http_outgoing.js:417 throw new TypeError(‘first argument must be a string or Buffer’); ^ TypeError: first argument must be a string or Buffer at ServerResponse.OutgoingMessage.write (_http_outgoing.js:417:11) at IncomingMessage.<anonymous> (C:\Users\xishvai\js\my\post-server.js:17:7) at IncomingMessage.EventEmitter.emit (events.js:101:17) at _stream_readable.js:896:16 at process._tickCallback (node.js:664:11)

浏览器的输出是正常的。 谁能告诉我为什么捏?

2 回复

17:7行是res.write(post.title);

浏览器通常会发送 /favicon.ico 请求来请求网站的fav icon。 你可以通过 console.log(req.url) 看到这个请求。

回到顶部