Home > Web Front-end > Front-end Q&A > How to set nodejs port

How to set nodejs port

PHPz
Release: 2023-04-26 09:53:39
Original
1956 people have browsed it

Node.js is a JavaScript runtime environment based on the V8 engine that can be used to develop server-side applications. During the development process, we may need to modify the port number of the Node.js application so that our application can run properly.

In Node.js, we can use the following code to set the port number:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
Copy after login

In the above code, we first define a constant hostname, which specifies the hostname we want to listen to. IP address, here we use the local IP address 127.0.0.1. Then we define a constant port, which specifies the port number we want to listen on. Here we use port number 3000. Next, we create an HTTP server and assign it to a variable server. We use the server.listen method to start the server and listen to the specified IP address and port number.

If we need to use a different port number, we only need to modify the port variable. For example, if we want to change the port number to 8080, we only need to change the value of the port variable to 8080.

During development, we can also pass the port number as a command line parameter to the Node.js application. For example, we can execute the following command on the command line to specify the port number as 8080:

node app.js --port=8080
Copy after login

In a Node.js application, we can obtain the command line parameters through the following code:

const args = process.argv.slice(2);
const port = args.find(arg => arg.includes('--port=')).split('=')[1];
Copy after login

The above code first uses process.argv to obtain the command line parameters, and uses the slice method to remove the first two parameters, leaving only our customized parameters. Then we use the find method to find the parameter containing --port=, and use the split method to separate the parameter name and parameter value.

To sum up, the port setting of Node.js is very simple. We can do it by modifying the code or passing command line parameters. In practical applications, we need to choose the most appropriate way to set the port number based on the actual situation.

The above is the detailed content of How to set nodejs port. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template