Skip to content

Instantly share code, notes, and snippets.

@adon-at-work
Created March 12, 2015 16:11
Show Gist options
  • Save adon-at-work/b84672a043355d7d67c8 to your computer and use it in GitHub Desktop.
Save adon-at-work/b84672a043355d7d67c8 to your computer and use it in GitHub Desktop.
IERG4210 Sample Code: Share the DB pool
var config = require('./shopXX-ierg4210.config.js'),
express = require('express'),
exphbs = require('express-handlebars'),
anyDB = require('any-db'),
frontEndRouter = require('./frontend.js');
var dbPool = anyDB.createPool(config.dbURI, {
min: 2, max: 20
});
var app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// frontend router runs last
app.use('/', frontEndRouter(dbPool));
app.listen(process.env.PORT || 3000, function () {
console.log('Example Server listening at port ' + (process.env.PORT || 3000));
});
var express = require('express');
module.exports = function (dbPool) {
var app = express.Router();
app.get('/', function (req, res) {
// async fetch data from SQL, render page when ready
dbPool.query('SELECT * FROM categories', function (error, results) {
if (error) {
console.error(error);
res.status(500).end();
return;
}
res.render('home', {
title: 'IERG4210 ShopXX',
cat: results.rows
});
});
});
app.get('/product', function (req, res) {
res.render('product', {
title: 'IERG4210 ShopXX Product'
});
});
return app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment