下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等。
搭建服务器页面输出hello world
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问 node.js bug,第二次访问/favicon.ico
console.log('访问');
response.write('hello,world 世界');
response.end();//不写则没有http协议尾
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
/*
启动服务
cmd下执行:
node 1_helloworld.js
浏览器访问:http://localhost:8000
*/