node专门提供了一个核心模块:http
http的职责就是帮你编写服务器
当客户端请求过来,就会自动触发服务器的request请求事件,然后执行回调处理
绑定端口号,启动服务器
var http=require('http')
var server = http.createServer()
server.on('request',function(request,response){
console.log("收到请求,请求路径是:"+request.url);
response.write("hello");
response.write("word");
response.end("222");
})
server.listen(3000,function(){
console.log("服务器启动成功了,使用localhost:3000或http://127.0.0.1:3000 访问");
})
request 请求对象
请求对象可以给客户端发送请求信息,例如路径
response 响应对象
响应对象可以给客户端发送响应消息
write可以使用多次,但最后一定要使用end来结束响应,否则客户端一直等待