Node quickly builds a simple web server

First, load the http core module

const http = require('http')

Second, use the http.createServer () method to create a Web server and return a Server instance

const server = http.createServer()

3. Turn on the server and respond

server.on('request', (req,res)=> {
	//req获取更多信息
	//res.end做出响应
   res.end('<p>hello</p>')
})

Four, bind port number 3000, start the server

server.listen(3000);
console.log('服务器启动成功了,可以通过 http://127.0.0.1:3000/ 或localhost:3000来进行访问')
  • req.headersGet request message, eg req.headers['键名']
  • req.urlGet address
  • req.methodRequest method
Published 128 original articles · 52 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_44523860/article/details/105161118