-
-
Save alessioalex/341d0c4e66da5935c89b420d789f6643 to your computer and use it in GitHub Desktop.
Async node-postgres sample code
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
/* | |
import express from 'express'; | |
import db from './core/db'; | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.get('/visits', (req, res, next) => { | |
db.connect(async ({ query } => { | |
await query('INSERT INTO visit (date) VALUES ($1)', new Date()); | |
const result = await query('SELECT COUNT(date) AS count FROM visit'); | |
res.send(`You are visitor number ${result.rows[0].count}.`); | |
}).catch(next); | |
}); | |
app.listen(port); | |
*/ | |
// callback version, just for demo purposes | |
const express = require('express'); | |
const db = require('./core/db'); | |
const errTo = require('errto'); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.get('/visits', (req, res, next) => { | |
db.connect(errTo(next, (query) => { | |
query('INSERT INTO visit (date) VALUES ($1)', new Date(), errTo(next, (result) => { | |
res.send(`You are visitor number ${result.rows[0].count}.`); | |
})); | |
})); | |
}); | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment