Express JS – app.listen vs server.listen
Last Updated :
21 Nov, 2023
In this article, we will learn about app.listen() & server.listen(), along with discussing the significant distinction that differentiates between app.listen() & server.listen().
What is app.listen() and server.listen() in Express JS?
app.listen(): In Express.js, app.listen() method is used to start a web server and listen to the connections on the specified host and port. This method is invoked on an instance of an Express application (app) created using express().
Syntax:
app.listen([port[, host[, backlog]]][, callback])
Parameters:
- port (optional): The port number on which the app will listen for incoming connections. If not specified, Express will use the default port (often 3000) or any other available port.
- host (optional): The hostname or IP address on which the server should listen. If not specified, Express will listen on any IPv4 address (0.0.0.0) or IPv6 address (::) depending on the environment.
- backlog (optional): The maximum length of the queue of pending connections. Default value is system-specific.
- callback (optional): A function to be called once the server starts listening. The callback receives no arguments.
Example : Simple server using app.listen()
Javascript
const express = require( 'express' );
const app = express();
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
|
Output:
Server is running on port 3000
server.listen(): In Node.js, server.listen() method is used to start a server and listens for incoming network connections on a specified port and optional hostname. This method is typically associated with the native http or https modules in Node.js.
The basic usage of server.listen() is to create an HTTP or HTTPS server using http.createServer() or https.createServer() respectively, and then calling server.listen() to start the server.
Syntax:
server.listen(port[, hostname][, backlog][, callback]);
Parameters:
- port (required): The port number on which the server listen for incoming connections.
- hostname (optional): The hostname or IP address on which the server should listen.
- backlog (optional): The maximum length of the queue of pending connections.
- callback (optional): A function to be called once the server starts listening.
Example: Simple server using http protocol
Javascript
const http = require( 'http' );
const PORT = 3000;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end( 'Hello, GFG!' );
});
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
|
Output:
Server is running on port 3000
Difference between app.listen() & server.listen():
Both app.listen() and server.listen() are methods used to create a server, but they differ in their usage and context:
It is a part of the Express.js framework module.
|
It is a part of the native HTTP module in Node.js.
|
Commbines the creation of an HTTP server and binds the Express application to that server in one method call.
|
Manually creates an HTTP server using http.createServer() and then using server.listen to bind it to a port.
|
Binds the Express application to an HTTP server, and handles the incoming HTTP requests through app’s routes and middleware.
|
Pprovides more control over the server instance and allows for custom handling of HTTP requests and responses using the created server object.
|
It requires less code and more focused for applications built using Express.js.
|
It offers lower-level access, which can be useful for specific server configurations.
|
It allows for setting specific Express-related configurations (app.set) alongside server initialization.
|
It offers flexibility to implement custom server behaviors, such as handling raw TCP connections, creating WebSocket servers, or integrating other protocols alongside HTTP.
|
Conclusion:
app.listen() is often used with frameworks like Express to create an HTTP server and bind the application to it. We don’t have to manually define everything as Express do it for us. While server.listen() is used when directly creating an HTTP server with the http module, allowing for more manual control over the server’s behavior. It is useful if you want to reuse the HTTP server, for example to run socket.io within the same HTTP server instance.
Share your thoughts in the comments
Please Login to comment...