npm i express
npm i -D nodemon
package.json
"scripts": {
"start":"node server.js",
"server":"nodemon server.js"
}
import express from 'express'; | |
const app = express(); | |
const host = process.env.HOST || 'localhost'; | |
const port = process.env.PORT || '3000'; | |
app.get('/', (req, res) => { | |
res.send(html); | |
}); | |
const server = app.listen(port, host, () => { | |
const host = server.address().address; | |
const port = server.address().port; | |
console.log('Server is running http://%s:%s', host, port); | |
}); | |
const handleShutdownGracefully = () => { | |
console.info("closing server gracefully..."); | |
app.close(() => { | |
console.info("server closed."); | |
// close db connections here or | |
// any other clean if required | |
process.exit(0); // if required | |
}); | |
} | |
process.on("SIGINT", handleShutdownGracefully); | |
process.on("SIGTERM", handleShutdownGracefully); | |
process.on("SIGHUP", handleShutdownGracefully); |