node版hello world

看了node有一段时间,一直很懒,没有整理,以后慢慢补上。

代码如下:

1
2
3
4
5
6
var http = require('http');
http.createServer(function(req,res){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello world!');
}).listen(3000);
console.log('Server started on localhost:3000');


到cmd运行node yourjsname.js,然后浏览器输入localhost:3000就行了,会出现的。


PS:
1、text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。
2、text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理。