diff --git a/database/connection.js b/database/connection.js index b93681e..8000d2a 100644 --- a/database/connection.js +++ b/database/connection.js @@ -1,10 +1,10 @@ // MODULES -const fs = require('fs'); -const mysql = require('mysql'); +const fs = require('fs') +const mysql = require('mysql') // Fetches and parses credentials file -let credentials_data = fs.readFileSync('./database/credentials.json'); -let credentials = JSON.parse(credentials_data); +let credentials_data = fs.readFileSync('./database/credentials.json') +let credentials = JSON.parse(credentials_data) const db = mysql.createConnection({ host: credentials.host, @@ -13,24 +13,4 @@ const db = mysql.createConnection({ database: credentials.database, }) -const getSpells = () => { - return new Promise((resolve, reject) => { - let query = - "SELECT * " + - "FROM spell"; - - db.query(query, (err, result, fields) => { - if (err) { - return reject; - } - resolve(result); - }) - - }) -} - -const closeConnection = (err) => { - db.end(); -} - -module.exports = { db, getSpells, closeConnection } \ No newline at end of file +module.exports = { db } \ No newline at end of file diff --git a/index.js b/index.js index 4e8fbbe..200ccd0 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,24 @@ // MODULES const express = require('express'); + const connection = require('./database/connection'); - const getSpells = connection.getSpells; - const closeConnection = connection.closeConnection; +const db = connection.db; + +// +const routes = require('./routes'); // CONSTANTS const port = 2814; // APP app = express(); -const server = app.listen(port, () => {console.log(`App listening on port ${port}`)}); +const server = app.listen( port, () => {console.log(`App listening on port ${port}`)}); -// getSpells().then((v) => { -// console.log(v); -// }).catch(r => { -// console.log(r); -// }) +app.use('/spells', routes.spells); -app.route('/spells') - .get((req, res, next) => { - getSpells().then(v => { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({spells : v})); - }) - }) - -// On process exit +// On process kill with SIGINT process.on('SIGINT', () => { - closeConnection(); + db.end(); server.close(); -}) \ No newline at end of file + console.log('Server closed'); +}) diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..5bfe085 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,5 @@ +const spells = require('./spells'); + +module.exports = { + spells, +} \ No newline at end of file diff --git a/routes/spells.js b/routes/spells.js new file mode 100644 index 0000000..775a94e --- /dev/null +++ b/routes/spells.js @@ -0,0 +1,32 @@ +const express = require('express'); +let router = express.Router(); + +const connection = require('../database/connection'); +const db = connection.db; + +getSpells = () => { + return new Promise((resolve, reject) => { + let query = + "SELECT * " + + "FROM spell"; + + db.query(query, (err, result) => { + if (err) { + return reject; + } + resolve(result); + }) + + }) +} + +router +.get('/', (req, res, next) => { + getSpells().then(v => { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({spells : v})); + }) +}) + + +module.exports = router \ No newline at end of file