Created
January 16, 2018 02:26
-
-
Save f3rn0s/efe2e1e827518a1e4887817cd8facf1e 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
| 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