PrefaceIt is very simple to create a server in node.js. You only need to use the http module and related APIs provided by node.js to create a small but complete web server. Compared with the process of building a web server in Java/Python/Ruby, it is much simpler. http modelTo create a web server based on Node.js, you must use the http module provided by Node.js. The http interface in Node.js is designed to support many features of the protocol that have traditionally been difficult to use. In particular, large, possibly block-encoded messages, the interface never buffers entire requests or responses, and users are able to stream data. In order to support all possible http applications, the http APIs of node.js are very low-level, which means that all the complex underlying problems are solved by node.js and encapsulated into modules and related APIs for us to use. You only need to follow the concise syntax of node.js to get started. Creating a ServerFirst, you need to import the http module, and then implement some functions through the relevant API. The following is a small but fully functional node web server. Before creating a server, it is worthwhile to go through these two APIs, or you can come back and look at them again. (1) Returns a newly created http.Server instance http.createServer([options][, requestListener])
(2) Start the HTTP server to listen for connections server.listen(port,callback)
(3) Printing a string on the screen (using chunks simply) response.write(chunk[, encoding][, callback])
hello.js // Import core module var http = require('http') // Create a server (the API returns an instance) var server = http.createServer() // Bind port number (3000) server.listen(3000, () => { console.log(`Server started successfully, waiting for client request...`) }) // Listen for requests from clients server.on('request', (request, response) => { //Client request path console.log(`Client request path is: ${request.url}`) // Respond to the client switch(request.url){ case '/': {//Browser default behavior response.write(`/`) break; } case '/index': {//Homepage response.write(`/index`) break; } case '/loging': {//Login page response.write(`/loging`) break; } } // Response completed response.end() }) We have finished writing the hello.js web server, now we start the server: 1. Open the cmd terminal and locate the hello.js file directory (IDE built-in terminal is also OK) 2. Start hello.js // Start the command node hello.js If the keyword [Server started successfully...] is displayed, it means the startup is successful, and then open the browser to prepare for testing. 3. Enter the local IP address, like this URL, and the port is the port configured for you, the default is 8000 Test Request Note that even if you don't write "/", the browser will add it by default. This concludes this article on the detailed steps of using node.js to create a web server. For more information about using node.js to create a web server, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Example code of the spread operator and its application in JavaScript
>>: About the usage and precautions of promise in javascript (recommended)
Preparation 1. The master and slave database vers...
Table of contents 1. Overview 2. Attributes 1. Ma...
<br />Question: Why is it not recommended to...
Table of contents Explanation of v-text on if for...
Table of contents Preface Function Overloading Ma...
1. Pull the image First, execute the following co...
Preface This article mainly introduces a problem ...
Table of contents 1. Open source warehouse manage...
This article shares the specific code for using j...
I recently installed Ubuntu 20.04 and found that ...
1. Alibaba Cloud selects the appropriate cloud se...
Keywords General The title cannot contain words l...
Because li is a block-level element and occupies ...
Table of contents Vite project build optimization...
Detailed explanation of mysql exists and not exis...