Last active
May 26, 2020 12:58
-
-
Save kluu1/a3ea06ca57a75e52effb1932f53572be to your computer and use it in GitHub Desktop.
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 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