nodeJS中初步实现服务器功能

步骤一 引入Http模块

const http =require('http');

步骤二 绑定请求事件 建立监听

在nodejs中req是httpincomingmessage的实例,

res是httpserverresponse的实例

  res.write('nihao');这个方法可以执行多次

  res.end('Hello'); 这个方法只能执行一次,里面可以写内容也可以不写内容

server.on('request',(req,res)=>{

    res.end('Hello');

});

server.listen(3000,'192.168.1.102',()=>{

console.log("在启动服务")

});

发布了368 篇原创文章 · 获赞 22 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/BianHuanShiZhe/article/details/104701124