Skip to content

Instantly share code, notes, and snippets.

@f3rn0s
Created January 16, 2018 02:26
Show Gist options
  • Select an option

  • Save f3rn0s/efe2e1e827518a1e4887817cd8facf1e to your computer and use it in GitHub Desktop.

Select an option

Save f3rn0s/efe2e1e827518a1e4887817cd8facf1e to your computer and use it in GitHub Desktop.
app.post('/users', function (req, res, next) {
const user = req.body
if (!user.name || !user.age) {
res.sendStatus(400)
return
}
client.query('INSERT INTO users (name, age) VALUES ($1, $2);', [user.name, user.age], function (err, result) {
if (err) {
// pass the error to the express error handler
return next(err)
}
res.sendStatus(200)
})
})
app.get('/users', function (req, res, next) {
client.query('SELECT name, age FROM users;', [], function (err, result) {
if (err) {
// pass the error to the express error handler
return next(err)
}
res.json(result.rows)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment