Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Created September 10, 2020 13:17
Show Gist options
  • Save cs-fedy/af8455952865c96b00d4879d1d36eb1d to your computer and use it in GitHub Desktop.
Save cs-fedy/af8455952865c96b00d4879d1d36eb1d to your computer and use it in GitHub Desktop.
basic express app
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const app = express();
app.use(morgan('dev'));
app.use(cors());
app.get('/', (req, res) => {
res.json({
message: "✌ Hello World! ✌"
});
});
function notFound(req, res, next) {
res.status(404);
const error = new Error(`Not Found - ${req.originalUrl}`);
next(error);
}
function errorHandler(err, req, res, next) {
res.status(res.statusCode || 500);
res.json({
message: err.message,
stack: err.stack
});
}
app.use(notFound);
app.use(errorHandler);
const port = process.env.PORT || 5050;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment