Created
October 24, 2017 17:05
-
-
Save N8BAUER/db37e754254c92798677bb794a0ee1f2 to your computer and use it in GitHub Desktop.
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
//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