app.listen()
Binds and listens for connections on the specified host and port.
Syntax
app.listen([port[, host]][, callback]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
port | number | No | The TCP port to listen on. |
host | string | No | The interface to bind. |
callback | function | No | Invoked once the server is listening. |
Returns
http.Server — The underlying Node HTTP server.
Examples
const app = express();
app.listen(3000, () => {
console.log('listening on http://localhost:3000');
});
Output
listening on http://localhost:3000
const server = app.listen(process.env.PORT || 3000);
process.on('SIGTERM', () => {
server.close(() => console.log('shut down cleanly'));
});
Output
shut down cleanly
Notes
Returns the Node `http.Server`, so you can call `server.close()` for
graceful shutdown or attach Socket.IO. For HTTPS or HTTP/2 create the
server manually with `http2.createSecureServer(app)` instead.