Skip to content

Instantly share code, notes, and snippets.

@coderinblack08
Created May 21, 2020 20:06
Show Gist options
  • Save coderinblack08/624f9cb360efe08c722478352a8f7e0d to your computer and use it in GitHub Desktop.
Save coderinblack08/624f9cb360efe08c722478352a8f7e0d to your computer and use it in GitHub Desktop.
Basic Express App
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