How to change nodejs default port

PHPz
Release: 2023-04-05 10:20:02
Original
1327 people have browsed it

With the popularity of Node.js, many people have begun to use it to develop web applications. In Node.js, the default port number used by Web applications is 80, but in many cases, we need to change the default port number, such as using a different port number in the development environment, or need to change the Web application in the production environment. The application publishes to a custom port number. So, in this article, we will explain how to change the default port of Node.js.

First method: specify the port number in the code

Node.js provides a built-in HTTP module, which can be used to create an HTTP server. We can use this module in our code to create a server and specify the port number.

The following is a simple sample code:

const http = require('http');

const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello, world!</h1>');
});

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

In the above code, we use process.env.PORT to obtain the port number in the system environment variable , if this variable does not exist, 3000 is used as the port number by default. When creating an HTTP server, we bind the server to the specified port through server.listen(port).

If we want to change the port number, we only need to modify the value of the port variable.

Second method: Specify the port number through command line parameters

In addition to specifying the port number in the code, we can also specify the port number through command line parameters. Node.js provides a built-in module process, which can be used to obtain command line parameters.

Here is a sample code:

const http = require('http');

const port = process.argv[2] || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Hello, world!</h1>');
});

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

In the above code, we used process.argv to get the command line parameters and passed the third parameter as The port number. If the third parameter is not present, 3000 is used as the port number by default.

The above are two methods of changing the default port of Node.js. Using these methods, you can easily change the default port number of the Node.js web application to meet the needs of different development and production environments.

The above is the detailed content of How to change nodejs default 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!