1建立一个js文件,名字为server.js
2.输入以下内容:
var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
hello();
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
function hello(){
console.log("hello console");
}
3.在此文件夹下打开命令行,输入 node servser.js
4.在浏览器输入http://localhost:8888/
5.控制台输出
6.第一个语句是程序启动就输出的,第二个是是访问了地址才出来的
7.http.createServer(onRequest).listen(8888);是一个异步函数。详情见以后的文章