Created
May 13, 2018 13:18
-
-
Save nigilan/c4981602a7cb6a1bf3752193c899ca49 to your computer and use it in GitHub Desktop.
To pass port in command line and also to close the server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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