Skip to content

Instantly share code, notes, and snippets.

@kluu1
Created April 2, 2020 12:47
Show Gist options
  • Select an option

  • Save kluu1/4c18c31279fde90eadd2894963cebd9a to your computer and use it in GitHub Desktop.

Select an option

Save kluu1/4c18c31279fde90eadd2894963cebd9a to your computer and use it in GitHub Desktop.
final
const express = require('express');
const bodyParser = require('body-parser');
const handleErrors = require('./middleware/handleErrors');
const { BadRequest } = require('./utils/errors');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.post('/post', async (req, res, next) => {
try {
const { title, author } = req.body;
if (!title || !author) {
throw new BadRequest('Missing required fields: title or author');
}
await db.posts.insert({ title, author });
next();
} catch (error) {
next(error);
}
});
app.use(handleErrors);
app.listen(port, () => console.log(`app is listening at http://localhost:${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment