当前位置:网站首页>Create a server with node

Create a server with node

2022-06-21 17:30:00 Yu'an_ ZhangDe

1、 Import http modular

// 1、 Import http modular 
const http = require('http')

2、 establish web Server instance

// 2、 establish web Server instance 
const server = http.createServer()

3、 Bind to the server instance request event , Listen for client requests

// 3、 Bind to the server request event 
server.on('request',(req,res)=>{
    // req.url  Get request address   The default is /
    const url = req.url
    console.log(url);
    // req.method  How to get the request   get/post
    const method = req.method
    console.log(method);

    // res.end()  The server responds to the client   Chinese is not supported 
    res.end('Hello world!')
})

4、  Start the server

// 4 、 Start the server 
server.listen(80,()=>{
    console.log('server running at http://localhost');
})

Operation result diagram

 

Installation configuration NodeJs Environmental Science

If you want to realize the above functions , First you need to install the configuration NodeJs Environmental Science , I put the link here , Please go to the rookie tutorial to see the specific tutorial , I won't go into details here .

Node.js Installation configuration | Novice tutorial (runoob.com)

原网站

版权声明
本文为[Yu'an_ ZhangDe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211517547592.html