What is the role of http in node
Execution Process1. Load the http module const http = require('http') 2. Use the http.createServer method to create a web server and return a server instance const server = http.createServer() 3. Provide services for data
server.on('request',function(){ console.log('Received client request') }) 4. Bind the port number and start the server server.listen(3000,()=> { console.log("The server started successfully and can be accessed through http://127.0.0.1:3000/") }) 5. node app.js started successfully
Building a basic web server requestThe code is as follows: const http = require('http') const server = http.createServer() // The request event handler needs to receive two parameters // The request object // The request object can obtain some request information from the client, such as the request path // The response object // The response object can be used to send a response message to the client server.on('request', function(request, response) { console.log('Received the client's request', 'The request path is: '+request.url) // The response object has a method, write, which can be used to send response data to the client. // write can be used multiple times, but the last time you must use end to end the response, otherwise the client will keep waiting for response.write("hello") response.write("nodejs") response.end() //Tell the client that I have finished speaking and you can show it to the user //Since our server capabilities are very weak now, no matter what the request is, it can only respond with hello nodejs // How to request different paths and respond to different results}) server.listen(3000,()=> { console.log("The server started successfully and can be accessed through http://127.0.0.1:3000/") }) The next step is to write a basic interface data to request Determine the home page data displayed on different pages a Page data …data that is different from the home page const http = require("http") const server = http.createServer() server.on('request',function(req,res){ res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'}) console.log("Request received, request path is: "+req.url) // res.write("heel") // res.write("world") //res.end() // The above method is more troublesome // You can send a response when using end // Send different request results according to different request paths //1. Get the requested path // req.url gets the path after the port number // That is to say, all urls start with / //2. Determine the path and process the response const url = req.url if(url=="/"){ const project = [ { name:"Apple", price:"18", }, { name:"Banana", price:"28", }, { name:"Watermelon", price:"20", }, { name:"xxx", price:"100", }, { name:"aaa", price:"100", } ] // Response data can only be binary data or string // Response data cannot be: number object array Boolean value res.end(JSON.stringify(project)) }else if(url=='/a'){ const a = [ { name:"Apple", price:"aa", }, { name:"Banana", price:"ww", }, { name:"Watermelon", price:"vv", }, { name:"wjcx", price:"bb", }, { name:"wdwa", price:"ww", } ] res.end(JSON.stringify(a)) } }) server.listen(3000,function(){ console.log("Server started successfully, you can access it now! http://127.0.0.1:3000/") }) This is the end of this article about the use of http module in node. For more relevant node http module content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to build LNMP environment on Ubuntu 20.04
>>: Two ways to open and close the mysql service
(I) Basic concepts of web page color matching (1) ...
Keepalived installation: cd <keepalived_source...
1. Create a shell script vim backupdb.sh Create t...
1. Delete folders Example: rm -rf /usr/java The /...
First time using docker to package and deploy ima...
This article shares the specific code of JavaScri...
The CSS position attribute specifies the element&...
MySQL master-slave setup MySQL master-slave repli...
Table of contents background Compile glibc 2.14 M...
Vertical Split Vertical splitting refers to the s...
Today I will share with you how to write a player...
1. flex-direction: (direction of element arrangem...
Table of contents 1. What is multi-instance 2. Pr...
Official website address: https://dev.mysql.com/d...
Table of contents Defining the HTML structure Inp...