Skip to content

Instantly share code, notes, and snippets.

@kluu1
Last active May 5, 2020 14:40
Show Gist options
  • Select an option

  • Save kluu1/44228c8f103550ce901e1063c3016eaa to your computer and use it in GitHub Desktop.

Select an option

Save kluu1/44228c8f103550ce901e1063c3016eaa to your computer and use it in GitHub Desktop.
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