|
1.建立一个名为test.js的文件,内容为:
// test.js
// 请求http模块
var http = require('http');
// 创建服务器
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("<h1>Node.js</h1>");
res.end('<p>Hello World</p>');
}).listen(3000);
// 控制台打印
console.log("start...");
console.log("HTTP server is listening at port 3000...");
2.在命令行工具中打开test.js所在的文件夹,然后输入:
node test
3.在浏览器中输入:
http://127.0.0.1:3000/
4.即可看到浏览器中显示:

至此,用Node.js创建的一个简单服务器就完成了。 |