Skip to content

Instantly share code, notes, and snippets.

@kluu1
Last active May 26, 2020 12:58
Show Gist options
  • Save kluu1/a3ea06ca57a75e52effb1932f53572be to your computer and use it in GitHub Desktop.
Save kluu1/a3ea06ca57a75e52effb1932f53572be to your computer and use it in GitHub Desktop.
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) => {
const { title, author } = req.body;
try {
if (!title || !author) {
throw new BadRequest('Missing required fields: title or author');
}
const post = await db.post.insert({ title, author });
res.json(post);
} catch (err) {
next(err)
}
});
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