Skip to content

Instantly share code, notes, and snippets.

@sajclarke
Last active August 25, 2018 18:21
Show Gist options
  • Save sajclarke/b79d34b5a692bc15881f11028be7212c to your computer and use it in GitHub Desktop.
Save sajclarke/b79d34b5a692bc15881f11028be7212c to your computer and use it in GitHub Desktop.
var mysql = require('mysql')
require('dotenv').config({path: __dirname + '/process.env'})
var con = mysql.createPool({
connectionLimit: 100,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: '8889',
database: 'calendar_test'
})
module.exports = con
--
-- Database: `calendar_test`
--
-- --------------------------------------------------------
--
-- Table structure for table `events`
--
CREATE TABLE `events` (
`id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `events`
--
INSERT INTO `events` (`id`, `title`, `start`, `end`) VALUES
(1, 'BIPA AGM', '2018-01-01 12:30:00', '2018-01-01 13:00:00');
import bodyParser from 'body-parser'
import express from 'express'
import path from 'path'
const app = express()
var con = require('./connected')
var cors = require('cors')
var forwarded = require('forwarded-for')
const fileUpload = require('express-fileupload')
app.set('trust proxy', 1) // trust first proxy
app.use(cors())
app.use(fileUpload())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
const staticFiles = express.static(path.join(__dirname, '../../client/build'))
app.use(staticFiles)
function connectionCheck() {
return new Promise((resolve, reject) => {
con.getConnection(function (err, connection) {
if (err) {
if (connection) connection.release()
reject(err)
} else {
resolve('success')
}
})
})
}
function connectionRelease() {
con.on('release', function (connection) {
console.log('Connection %d released', connection.threadId)
})
}
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/', function (req, res) {
res.json({ Error: true, Message: 'Not allowed' })
})
app.get('/events', function (req, res) {
var sql = 'SELECT * FROM events'
con.query(sql, function (err, rows) {
if (err) {
res.json({ Error: true, Message: 'Error Execute Sql', err })
} else {
res.json(rows)
}
})
})
// any routes not picked up by the server api will be handled by the react router
app.use('/*', staticFiles)
app.set('port', process.env.PORT || 3001)
app.listen(app.get('port'), () => {
console.log(`Listening on ${app.get('port')}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment