Skip to content

Instantly share code, notes, and snippets.

@N8BAUER
Created October 24, 2017 17:05
Show Gist options
  • Save N8BAUER/078c1395858ab273c49915052d27bbe3 to your computer and use it in GitHub Desktop.
Save N8BAUER/078c1395858ab273c49915052d27bbe3 to your computer and use it in GitHub Desktop.
//TO INSTALL: npm install express cors body-parser morgan monk
const http = require('http')
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const cors = require('cors')
const app = module.exports = express()
const server = http.createServer(app)
const port = parseInt(process.env.PORT || 3000)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(morgan('dev'))
app.use(cors({origin: true}))
// ADD (MOUNT) YOUR MIDDLEWARE (ROUTES) HERE
// ^^^ Example: app.use('/v1/kitten', require('./routes/kitten'))
app.use(require('./routes/users'))
app.use(notFound)
app.use(errorHandler)
server.listen(port)
.on('error', console.error.bind(console))
.on('listening', console.log.bind(console, 'Listening on ' + port));
function notFound(req, res, next) {
res.status(404)
.send({error: 'Url not found', status: 404, url: req.url})
}
function errorHandler(err, req, res, next) {
console.error('ERROR', err)
res.status(500)
.send({error: err, url: req.url, status: 500})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment