Last active
May 5, 2020 14:40
-
-
Save kluu1/44228c8f103550ce901e1063c3016eaa 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 app = express(); | |
| const port = 3000; | |
| app.use(bodyParser.json()); | |
| app.post('/post', async (req, res) => { | |
| const { title, author } = req.body; | |
| if (!title || !author) { | |
| return res.status(400).json({ | |
| status: 'error', | |
| message: 'Missing required fields: title or author' | |
| }); | |
| } | |
| try { | |
| const post = await db.post.insert({ title, author }); | |
| res.json(post); | |
| } catch (error) { | |
| return res.status(500).json({ | |
| status: 'error', | |
| message: 'Internal Server Error' | |
| }); | |
| } | |
| }); | |
| 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