Created
May 21, 2020 20:06
-
-
Save coderinblack08/624f9cb360efe08c722478352a8f7e0d to your computer and use it in GitHub Desktop.
Basic Express App
This file contains hidden or 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 cors = require('cors'); | |
const volleyball = require('volleyball'); | |
require('dotenv').config(); | |
const app = express(); | |
const port = process.env.PORT || 3030; | |
// Built in 'Body Parser' middleware for express | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
app.use(volleyball); | |
app.use(cors()); | |
app.get('/', (req, res) => { | |
res.json({ | |
message: 'Welcome to the backend β¨ππ! π Welcome to the main route!' | |
}); | |
}); | |
const notFound = (req, res, next) => { | |
res.status(404); | |
const error = new Error('Not Found - ' + req.originalUrl); | |
next(error); | |
} | |
const errorHandler = (err, req, res, next) => { | |
res.status(res.statusCode || 500); | |
res.json({ | |
message: err.message, | |
stack: err.stack | |
}); | |
} | |
app.use(notFound); | |
app.use(errorHandler); | |
app.listen(port, (req, res) => console.log(`Backend running on port ${port}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment