Created
April 2, 2020 12:47
-
-
Save kluu1/4c18c31279fde90eadd2894963cebd9a to your computer and use it in GitHub Desktop.
final
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) => { | |
| 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