下面将带领大家一步步学习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
*/
本文引导读者学习如何使用Node.js搭建服务器,响应GET/POST请求,并介绍如何处理页面输出Hello,World。通过创建一个简单的HTTP服务器并监听8000端口,当访问特定URL时,服务器将返回hello,world世界。此外,还提及了消除第二次访问/favicon.ico时的bug问题。
335

被折叠的 条评论
为什么被折叠?



