Open In App

How to Listen on Port 80 with Node.js ?

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Listening on port 80, the default HTTP port, is a common requirement for web servers because it allows users to access the service without specifying the port number in the URL. However, configuring Node.js to listen on port 80 involves some considerations, especially regarding permissions and security. This article guides you through the steps to set up a Node.js application to listen on port 80, including handling permissions, security best practices, and alternative approaches.

Why Port 80?

Port 80 is the standard port for HTTP traffic, making it the default port for web browsers when accessing a website. Listening on this port allows users to access your website or application using a simple URL like http://example.com without needing to specify a port number.

Syntax:

Importing ‘HTTP’ module

const http = require('http');

Creating Server

const server = http.createServer((req,res)=>{
// Handle request and response
});

Specify Port Number and hostname and set the server to listen to it.

server.listen(port,hostname,callback); 

What happens when we listen to port 80?

The default port for HTTP is 80 – Generally, most web browsers listen to the default port. The syntax for a server URL is:

http://{hostname}:{port}/

So if we do not mention the port in the server URL, then by default it takes it as  80. To put it simply, http://localhost/  is exactly same as  http://localhost:80/

Example: Below is the code implementation for creating a server in node and making it listen to port 80.

JavaScript
// File name - port_80.js
// Importing 'http' module 
const http = require('http');

// Setting Port Number as 80 
const port = 80;

// Setting hostname as the localhost
// NOTE: You can set hostname to something 
// else as well, for example, say 127.0.0.1
const hostname = 'localhost';

// Creating Server 
const server = http.createServer((req,res)=>{

    // Handling Request and Response 
    res.statusCode=200;
    res.setHeader('Content-Type', 'text/plain')
    res.end("Welcome to Geeks For Geeks")
});

// Making the server to listen to required
// hostname and port number
server.listen(port,hostname,()=>{

    // Callback 
    console.log(`Server running at http://${hostname}:${port}/`);
});

Step to Run Application: Run the application using the following command from the root directory of the project

node port_80.js

The Server is running at http://localhost:80/

Output: Now run http://localhost:80/ in the browser.

Welcome to Geeks For Geeks

Conclusion

Listening on port 80 with Node.js requires careful consideration of security and permissions. While you can run your application with elevated privileges, using a reverse proxy like Nginx or authbind is often a better approach for security reasons. By following best practices and understanding the implications of binding to privileged ports, you can ensure your Node.js application is both accessible and secure.



Next Article

Similar Reads