Entry Point for Sample NodeJS App for Deployment
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 bodyParser = require('body-parser'); | |
const helmet = require('helmet'); | |
const compression = require('compression'); | |
const app = express(); | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(express.static('public')); | |
let todoList = ["Buy Eggs"]; | |
app.post('/add', (req, res) => { | |
const todoItem = req.body.todo; | |
todoList.push(todoItem); | |
res.json({success: true}); | |
}); | |
app.get('/list', (_, res) => { | |
res.json(todoList); | |
}); | |
app.use((req, res, next) => { | |
res.status(404).send('404: Page not found'); | |
}); | |
app.use((err, req, res, next) => { | |
console.error(err.stack); | |
res.status(500).send('500: Internal server error'); | |
}); | |
app.use(helmet()); | |
app.use(compression()); | |
const port = process.env.PORT || 3000; | |
app.listen(port, () => { | |
console.log(`Server started on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment