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)
How to install MySQL 5.7.18 on Linux 1. Download ...
Preface Docker can configure environment variable...
If you want to adjust the size and number of Inno...
Solution: Directly in the directory where you dow...
Table of contents In JavaScript , there are sever...
Table of contents Docker Installation Nvidia-dock...
This article shares the specific code for impleme...
Harbor is an enterprise-level registry server for...
This article uses an example to illustrate the ex...
First, you need to determine which fields or fiel...
This article example shares the specific code of ...
The main contents of this article are as follows:...
The format is simple: proxy_pass URL; The URL inc...
This article shares the MySQL 5.7.18 MSI installa...
Maybe I started to think wrongly, and my descript...