大家好,我今天刚开始学习 node.js 并在互联网上搜索了很多东西,然后尝试在 node.js 中编码我使用这两个代码向我显示相同的结果,但最后一个是在我的浏览器上显示错误诸如“找不到页面”之类的东西。所以请向我解释为什么?
// JScript source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
这是有效的,但是
// Include http module.
var http = require("http");
// Create the server. Function passed as parameter is called on every request made.
// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
// Attach listener on end event.
// This event is called when client sent all data and is waiting for response.
request.on("end", function () {
// Write headers to the response.
// 200 is HTTP status code (this one means success)
// Second parameter holds header fields in object
// We are sending plain text, so Content-Type should be text/plain
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// Send data and end response.
response.end('Hello HTTP!');
});
}).listen(1337, "127.0.0.1");
这个不工作
为什么?
最后一个不起作用的链接 http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/ 谢谢你的所有答案,但我仍然不明白这些问题. 最后一个不工作的只有 request.on?