Skip to content

Instantly share code, notes, and snippets.

@alessioalex alessioalex/server.js forked from koistya/server.js
Last active May 27, 2016

Embed
What would you like to do?
Async node-postgres sample code
/*
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
You can’t perform that action at this time.