Node入门--7-->Http创建服务器

  • Why:可以搭建属于自己服务器
  • What:

    

  • How
// 1.通过http模块,创建本地服务器
var http = require('http');

// 2.通过对象创建服务器方法
var server = http.createServer(function (req,res) {

console.log("客户端向服务器发送请求" + req.url);
res.writeHead(200, {"Content-type":"text/plain"});
res.end("Server is Working!");//可以返回html,json等文件
});

// 3.服务对象监听服务器地址以及端口号
server.listen(8888, '127.0.0.1');
原文地址:https://www.cnblogs.com/Afanty/p/6924898.html