Skip to content

Instantly share code, notes, and snippets.

@nigilan
Created May 13, 2018
Embed
What would you like to do?
To pass port in command line and also to close the server
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send({"mess": "hello world"});
});
const portArrayPointer = process.argv.indexOf("--port");
if (portArrayPointer) {
app.set('port', process.argv[portArrayPointer+1] || 3001);
}
const server = app.listen(app.get('port'), () => {
console.log("App listening on port-->>>",app.get('port'));
});
server.close(() => {
console.log("server closed");
});
// node app --port 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment