使用node.js定义一个web服务器

var http = require("http");
//创建一个服务器 固定方法 creatServer()
//request  response
//回调函数里面的代码 需要通过浏览器请求服务器才可以执行
var server = http.createServer(function(req, res){
        //头文件设置,中文显示
        res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
        console.log(req.url);
        //向浏览器回应信息
        /*res.write("hello nodejs")*/  //向客户端写入内容
        res.end("一个好的程序员是那种过单行线马路都要往两边看的人");//结束响应
}).listen(9001, function(){
        console.log("success");
}); //目的:直观显示服务是否开启
//在浏览器地址栏中输入:localhost:9001或者http://本机ip:9001

猜你喜欢

转载自www.cnblogs.com/luowenshuai/p/9286253.html