Skip to content

Instantly share code, notes, and snippets.

@AryanJ-NYC
Created October 17, 2016 03:02
Show Gist options
  • Save AryanJ-NYC/eb4476bdecafb0188584c74d1a0f0a63 to your computer and use it in GitHub Desktop.
Save AryanJ-NYC/eb4476bdecafb0188584c74d1a0f0a63 to your computer and use it in GitHub Desktop.
const config = require('./config.js'),
mysql = require('mysql'),
mySqlConnection = mysql.createConnection({
host: '149.4.68.217',
user: config.DB_READ_USER,
database: 'bpl_performance_lab',
password: config.DB_PW
});
exports.connect = function (callback) {
mySqlConnection.connect(function (err) {
if (err) {
console.error('Error connecting: ' + err.stack);
return;
}
console.log(`Connected as id ${mySqlConnection.threadId}`);
callback(err, mySqlConnection);
});
};
'use strict';
const express = require('express'),
app = express(),
database = require('./config/database.js'),
squel = require('squel'),
port = process.env.PORT || 8000;
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({ error: { message: err.message }});
});
app.get('/api/weather', function (req, res) {
database.connect(function (err, connection) {
if (err) throw err;
let weatherQuery = squel.select({ autoQuoteTableNames: true, autoQuoteFieldNames: true })
.from('weather').toString();
connection.query(weatherQuery, function (err, rows) {
res.send(rows);
});
})
});
app.listen(port, function () {
console.log(`App running on port: ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment