Created
July 3, 2015 18:10
-
-
Save baniol/cf181830aee7c276f45f to your computer and use it in GitHub Desktop.
Express boilerplate, router files
This file contains 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
var express = require('express'); | |
var router = express.Router(); | |
/* GET home page. */ | |
router.get('/', function(req, res, next) { | |
res.render('index', { title: 'Express' }); | |
}); | |
module.exports = router; |
This file contains 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
'use strict'; | |
var mysql = require('mysql'); | |
var express = require('express'); | |
var router = express.Router(); | |
/** | |
* Returns connection to the mysql database | |
* | |
* @return {Object} mysql connection | |
*/ | |
function getConnection () { | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
database : 'loadtest', | |
user : 'root', | |
password : 'szapo123' | |
}); | |
return connection; | |
} | |
function getUsers (cb) { | |
var connection = getConnection(); | |
connection.connect(); | |
var sqlQuery = 'SELECT * FROM testdata'; | |
connection.query(sqlQuery, function(err, rows, fields) { | |
if (err) { | |
cb(err); | |
} else { | |
cb(null, rows); | |
} | |
}); | |
connection.end(); | |
} | |
/* GET users listing. */ | |
router.get('/', function(req, res, next) { | |
getUsers(function (err, data) { | |
if (err) { | |
next(new Error('DB error: ' + err)); | |
} | |
else { | |
res.json(data); | |
} | |
}); | |
}); | |
router.get('/some', function(req, res, next) { | |
res.send('kuku'); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment