From 4127228970f4534b5755ee9ef3db30b7c84f2890 Mon Sep 17 00:00:00 2001 From: andygout Date: Thu, 6 Oct 2016 23:00:09 +0100 Subject: [PATCH] Implement: ES6 modules syntax --- app.js | 20 ++++++++++---------- bin/www | 2 +- config.js | 2 +- init.js | 3 +++ lib/query.js | 9 ++++----- package.json | 2 ++ server/controllers/index.js | 3 +++ server/controllers/productions/create.js | 8 ++++---- server/controllers/productions/delete.js | 8 ++++---- server/controllers/productions/edit.js | 8 ++++---- server/controllers/productions/index.js | 17 ++++++++--------- server/controllers/productions/list.js | 10 ++++++++++ server/controllers/productions/new.js | 6 +++--- server/controllers/productions/show.js | 8 ++++---- server/controllers/productions/update.js | 8 ++++---- server/models/database.js | 5 ++--- server/routes/index.js | 23 ++++++++++++----------- setup.js | 3 +++ 18 files changed, 82 insertions(+), 63 deletions(-) create mode 100644 init.js create mode 100644 server/controllers/index.js create mode 100644 server/controllers/productions/list.js create mode 100644 setup.js diff --git a/app.js b/app.js index 53f34f8b..a28e18b0 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,12 @@ -const bodyParser = require('body-parser'); -const cookieParser = require('cookie-parser'); -const express = require('express'); -const exphbs = require('express-handlebars'); -const favicon = require('serve-favicon'); -const logger = require('morgan'); -const path = require('path'); -const routes = require('./server/routes/index'); -const sassMiddleware = require('node-sass-middleware'); +import bodyParser from 'body-parser'; +import cookieParser from 'cookie-parser'; +import express from 'express'; +import exphbs from 'express-handlebars'; +import favicon from 'serve-favicon'; +import logger from 'morgan'; +import path from 'path'; +import sassMiddleware from 'node-sass-middleware'; +import { router } from './server/routes'; const app = express(); const hbs = exphbs.create({ defaultLayout: 'main', extname: '.html' }) @@ -31,7 +31,7 @@ app.use( ); app.use(express.static(path.join(__dirname, './client', 'public'))); -app.use('/', routes); +app.use('/', router); // Catch 404 and forward to error handler app.use(function (req, res, next) { diff --git a/bin/www b/bin/www index 5d997dc8..a3509fbf 100644 --- a/bin/www +++ b/bin/www @@ -1,7 +1,7 @@ #!/usr/bin/env node // Module dependencies. -const app = require('../app'); +const app = require('../init'); const http = require('http'); // Normalize a port into a number, string, or false. diff --git a/config.js b/config.js index 05764b39..767ab512 100644 --- a/config.js +++ b/config.js @@ -1,3 +1,3 @@ const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/theatrebase_nodejs'; -module.exports = connectionString; +export { connectionString }; diff --git a/init.js b/init.js new file mode 100644 index 00000000..7cbdafff --- /dev/null +++ b/init.js @@ -0,0 +1,3 @@ +require('./setup'); + +module.exports = require('./app'); diff --git a/lib/query.js b/lib/query.js index 225b42ea..7be3bc3f 100644 --- a/lib/query.js +++ b/lib/query.js @@ -1,8 +1,7 @@ -const pg = require('pg'); -const path = require('path'); -const connectionString = require(path.join(__dirname, '../', 'config')); +import pg from 'pg'; +import { connectionString } from '../config'; -module.exports = function (queryText, callback) { +export default function (queryText, callback) { pg.connect(connectionString, function (err, client, release) { if (err) return callback(err); @@ -16,4 +15,4 @@ module.exports = function (queryText, callback) { return callback(null, rows); }); }); -}; +} diff --git a/package.json b/package.json index f0663a72..0bf5ce37 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "author": "https://github.com/andygout", "license": "MS-RSL", "dependencies": { + "babel-plugin-transform-es2015-modules-commonjs": "^6.16.0", + "babel-register": "^6.16.3", "body-parser": "^1.10.2", "cookie-parser": "^1.3.3", "debug": "^2.1.1", diff --git a/server/controllers/index.js b/server/controllers/index.js new file mode 100644 index 00000000..a701f3f4 --- /dev/null +++ b/server/controllers/index.js @@ -0,0 +1,3 @@ +import * as productions from './productions'; + +export { productions } diff --git a/server/controllers/productions/create.js b/server/controllers/productions/create.js index c7ce5690..382253ff 100644 --- a/server/controllers/productions/create.js +++ b/server/controllers/productions/create.js @@ -1,7 +1,7 @@ -const format = require('pg-format'); -const query = require('../../../lib/query'); +import format from 'pg-format'; +import query from '../../../lib/query'; -module.exports = (req, res, next) => { +export default function (req, res, next) { const data = { title: format.literal(req.body.title) }; @@ -12,4 +12,4 @@ module.exports = (req, res, next) => { if (err) return next(err); res.redirect(`/productions/${production.id}`); }); -}; +} diff --git a/server/controllers/productions/delete.js b/server/controllers/productions/delete.js index 4dea3c20..eaae5434 100644 --- a/server/controllers/productions/delete.js +++ b/server/controllers/productions/delete.js @@ -1,7 +1,7 @@ -const format = require('pg-format'); -const query = require('../../../lib/query'); +import format from 'pg-format'; +import query from '../../../lib/query'; -module.exports = (req, res, next) => { +export default function (req, res, next) { const id = format.literal(req.params.id); const queryText = `DELETE FROM productions WHERE id=${id}`; @@ -10,4 +10,4 @@ module.exports = (req, res, next) => { if (err) return next(err); res.redirect('/'); }); -}; +} diff --git a/server/controllers/productions/edit.js b/server/controllers/productions/edit.js index abd6199d..2efeb8a6 100644 --- a/server/controllers/productions/edit.js +++ b/server/controllers/productions/edit.js @@ -1,7 +1,7 @@ -const format = require('pg-format'); -const query = require('../../../lib/query'); +import format from 'pg-format'; +import query from '../../../lib/query'; -module.exports = (req, res, next) => { +export default function (req, res, next) { const id = format.literal(req.params.id); const queryText = `SELECT * FROM productions WHERE id=${id}`; @@ -17,4 +17,4 @@ module.exports = (req, res, next) => { res.render('form', Object.assign({}, content, production)); }); -}; +} diff --git a/server/controllers/productions/index.js b/server/controllers/productions/index.js index 3372b9e7..959c6371 100644 --- a/server/controllers/productions/index.js +++ b/server/controllers/productions/index.js @@ -1,10 +1,9 @@ -const query = require('../../../lib/query'); +import newRoute from './new'; +import createRoute from './create'; +import editRoute from './edit'; +import updateRoute from './update'; +import deleteRoute from './delete'; +import showRoute from './show'; +import listRoute from './list'; -module.exports = (req, res, next) => { - const queryText = 'SELECT * FROM productions ORDER BY id ASC'; - - query(queryText, function (err, productions) { - if (err) return next(err); - res.render('index', { content: JSON.stringify(productions) }); - }); -}; +export { newRoute, createRoute, editRoute, updateRoute, deleteRoute, showRoute, listRoute } diff --git a/server/controllers/productions/list.js b/server/controllers/productions/list.js new file mode 100644 index 00000000..003ffec6 --- /dev/null +++ b/server/controllers/productions/list.js @@ -0,0 +1,10 @@ +import query from '../../../lib/query'; + +export default function (req, res, next) { + const queryText = 'SELECT * FROM productions ORDER BY id ASC'; + + query(queryText, function (err, productions) { + if (err) return next(err); + res.render('index', { content: JSON.stringify(productions) }); + }); +} diff --git a/server/controllers/productions/new.js b/server/controllers/productions/new.js index 90b49355..5820fb4d 100644 --- a/server/controllers/productions/new.js +++ b/server/controllers/productions/new.js @@ -1,6 +1,6 @@ -const query = require('../../../lib/query'); +import query from '../../../lib/query'; -module.exports = (req, res) => { +export default function (req, res) { const content = { pageTitle: 'New production', formAction: '/productions', @@ -8,4 +8,4 @@ module.exports = (req, res) => { } res.render('form', content); -}; +} diff --git a/server/controllers/productions/show.js b/server/controllers/productions/show.js index 33251ec9..313421d9 100644 --- a/server/controllers/productions/show.js +++ b/server/controllers/productions/show.js @@ -1,7 +1,7 @@ -const format = require('pg-format'); -const query = require('../../../lib/query'); +import format from 'pg-format'; +import query from '../../../lib/query'; -module.exports = (req, res, next) => { +export default function (req, res, next) { const id = format.literal(req.params.id); const queryText = `SELECT * FROM productions WHERE id=${id}`; @@ -10,4 +10,4 @@ module.exports = (req, res, next) => { if (err) return next(err); res.render('show', production); }); -}; +} diff --git a/server/controllers/productions/update.js b/server/controllers/productions/update.js index 6cef1be2..87704fd4 100644 --- a/server/controllers/productions/update.js +++ b/server/controllers/productions/update.js @@ -1,7 +1,7 @@ -const format = require('pg-format'); -const query = require('../../../lib/query'); +import format from 'pg-format'; +import query from '../../../lib/query'; -module.exports = (req, res, next) => { +export default function (req, res, next) { const id = req.params.id; const data = { @@ -15,4 +15,4 @@ module.exports = (req, res, next) => { if (err) return next(err); res.redirect(`/productions/${id}`); }); -}; +} diff --git a/server/models/database.js b/server/models/database.js index 59d9ff73..e625ed05 100644 --- a/server/models/database.js +++ b/server/models/database.js @@ -1,6 +1,5 @@ -const pg = require('pg'); -const path = require('path'); -const connectionString = require(path.join(__dirname, '../', '../', 'config')); +import pg from 'pg'; +import connectionString from '../../config'; const client = new pg.Client(connectionString); client.connect(); diff --git a/server/routes/index.js b/server/routes/index.js index 6143e3d1..93cee1ed 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,5 +1,6 @@ -const express = require('express'); -const methodOverride = require('method-override'); +import express from 'express'; +import methodOverride from 'method-override'; +import * as controllers from '../controllers'; const router = express.Router(); @@ -12,15 +13,15 @@ router.use(methodOverride(function (req, res) { })); // Home -router.get('/', require('../controllers/productions/index')); +router.get('/', controllers.productions.listRoute); // Productions -router.get('/productions/new', require('../controllers/productions/new')); -router.post('/productions', require('../controllers/productions/create')); -router.get('/productions/:id/edit', require('../controllers/productions/edit')); -router.post('/productions/:id', require('../controllers/productions/update')); -router.delete('/productions/:id', require('../controllers/productions/delete')); -router.get('/productions/:id', require('../controllers/productions/show')); -router.get('/productions', require('../controllers/productions/index')); +router.get('/productions/new', controllers.productions.newRoute); +router.post('/productions', controllers.productions.createRoute); +router.get('/productions/:id/edit', controllers.productions.editRoute); +router.post('/productions/:id', controllers.productions.updateRoute); +router.delete('/productions/:id', controllers.productions.deleteRoute); +router.get('/productions/:id', controllers.productions.showRoute); +router.get('/productions', controllers.productions.listRoute); -module.exports = router; +export { router }; diff --git a/setup.js b/setup.js new file mode 100644 index 00000000..707bbcd9 --- /dev/null +++ b/setup.js @@ -0,0 +1,3 @@ +require('babel-register')({ + plugins: ["transform-es2015-modules-commonjs"] +});