Last active
February 16, 2021 00:49
-
-
Save frankstepanski/be1fb6e35060efc49794d75390cac6c6 to your computer and use it in GitHub Desktop.
Basic Express Boilerplate
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
require('dotenv').config() | |
const express = require('express') | |
const morgan = require('morgan') | |
const cors = require('cors') | |
const helmet = require('helmet') | |
const { NODE_ENV } = require('./config') | |
const app = express() | |
const morganOption = (NODE_ENV === 'production') | |
? 'tiny' | |
: 'common'; | |
app.use(morgan(morganOption)) | |
app.use(cors()) | |
app.use(helmet()) | |
app.get('/', (req, res) => { | |
res.send('Hello, world!') | |
}) | |
app.use(function errorHandler(error, req, res, next) { | |
let response | |
if (NODE_ENV === 'production') { | |
response = { error: { message: 'server error' } } | |
} else { | |
console.error(error) | |
response = { message: error.message, error } | |
} | |
res.status(500).json(response) | |
}) | |
module.exports = app |
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
module.exports = { | |
PORT: process.env.PORT || 8000, | |
NODE_ENV: process.env.NODE_ENV || 'development', | |
} |
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
{ | |
"name": "express-boilerplate", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "mocha --require test/setup.js", | |
"dev": "nodemon src/server.js", | |
"start": "node src/server.js", | |
"predeploy": "npm audit", | |
"deploy": "git push heroku master" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"cors": "^2.8.5", | |
"dotenv": "^6.2.0", | |
"express": "^4.16.4", | |
"helmet": "^3.15.0", | |
"morgan": "^1.9.1" | |
}, | |
"devDependencies": { | |
"chai": "^4.2.0", | |
"mocha": "^5.2.0", | |
"nodemon": "^1.18.9", | |
"supertest": "^3.4.1" | |
} | |
} |
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 app = require('./app') | |
const { PORT } = require('./config') | |
app.listen(PORT, () => { | |
console.log(`Server listening at http://localhost:${PORT}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment