[node.js] 03-http module

Table of contents

1. What is the http module

2. Create a basic WEB server

Three, req request object

Four, res response object

5. Respond to different JSON content according to different urls


1. What is the http module

        The http module is a module officially provided by Node.js to create a web server. Through the http.createServer() method provided by the http module , an ordinary computer can be conveniently turned into a web server to provide web resource services to the outside world.

2. Create a basic WEB server

step:

1. Import the http module
2. Create a web server instance 3. Bind the request event
to the server instance and listen to the client's request 4. Start the server

code:

//1. 导入http模块
const http = require('http')

//2. 创建web服务器实例
const server = http.createServer()

//3. 为服务器实例绑定 request 事件,监听客户端的请求
server.on('request', function(req, res) {
	console.log('Someone visit our web server.')
})

//4. 启动服务器
server.listen(80, function() {
	console.log('Server running at http://127.0.0.1:80')
})

Three, req request object

        As long as the server receives the client's request, it will call the request event handler bound to the server through server.on(). If you want to access data or properties related to the client in the event handler, you can use the following methods:

const http = require('http')
const server = http.createServer()

//req是请求对象,包含了与客户端相关的数据和属性
server.on('request', req => {
	const url = req.url //客户端请求的URL地址
	const method = req.method //客户端的请求类型
	console.log(`Your request url is ${url}, and request method is ${method}`)
})

server.listen(80, () => {
	console.log('Server running at http://127.0.0.1:80')
})

Run the code at this point, and then use postman to send a post request:

 You can see the server print information as follows:

Four, res response object

        In the server's request event handler, if you want to access server-related data or attributes, you can use the following methods:

When using res.end() to send Chinese to the client, there will be Chinese garbled characters. At this time, you need to set the Chinese encoding format:

5. Respond to different JSON content according to different urls

step:

1. Get the requested url address

2. Set the default response content to 404 Not found

3. Judging user requests and responding to different JSONs

4. Set the Content-Type response header to prevent Chinese garbled characters

5. Use res.end() to respond the content to the client

code:

const http = require('http')
const server = http.createServer()

server.on('request', (req, res) => {
	//1. 获取请求的URL地址
	const url = req.url

	//2. 设置默认的响应内容
	let content = '404 Not Found'

	//3.判断用户请求响应JSON
	if (url === '/') {
		content = { name: '小红', age: 18 }
	} else {
		content = { name: '小黑', age: 20 }
	}
	//4. 设置响应头
	res.setHeader('Content-Type', 'application/json')

	//5. 向客户端响应的JSON内容
	res.end(JSON.stringify(content))
})

server.listen(80, () => {
	console.log('Server running at http://127.0.0.1:80')
})

Guess you like

Origin blog.csdn.net/ChaoChao66666/article/details/131911808