Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement: ES6 modules syntax #16

Merged
merged 1 commit into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -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' })
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion bin/www
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/theatrebase_nodejs';

module.exports = connectionString;
export { connectionString };
3 changes: 3 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./setup');

module.exports = require('./app');
9 changes: 4 additions & 5 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -16,4 +15,4 @@ module.exports = function (queryText, callback) {
return callback(null, rows);
});
});
};
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as productions from './productions';

export { productions }
8 changes: 4 additions & 4 deletions server/controllers/productions/create.js
Original file line number Diff line number Diff line change
@@ -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)
};
Expand All @@ -12,4 +12,4 @@ module.exports = (req, res, next) => {
if (err) return next(err);
res.redirect(`/productions/${production.id}`);
});
};
}
8 changes: 4 additions & 4 deletions server/controllers/productions/delete.js
Original file line number Diff line number Diff line change
@@ -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}`;
Expand All @@ -10,4 +10,4 @@ module.exports = (req, res, next) => {
if (err) return next(err);
res.redirect('/');
});
};
}
8 changes: 4 additions & 4 deletions server/controllers/productions/edit.js
Original file line number Diff line number Diff line change
@@ -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}`;
Expand All @@ -17,4 +17,4 @@ module.exports = (req, res, next) => {

res.render('form', Object.assign({}, content, production));
});
};
}
17 changes: 8 additions & 9 deletions server/controllers/productions/index.js
Original file line number Diff line number Diff line change
@@ -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 }
10 changes: 10 additions & 0 deletions server/controllers/productions/list.js
Original file line number Diff line number Diff line change
@@ -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) });
});
}
6 changes: 3 additions & 3 deletions server/controllers/productions/new.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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',
submitValue: 'Create production'
}

res.render('form', content);
};
}
8 changes: 4 additions & 4 deletions server/controllers/productions/show.js
Original file line number Diff line number Diff line change
@@ -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}`;
Expand All @@ -10,4 +10,4 @@ module.exports = (req, res, next) => {
if (err) return next(err);
res.render('show', production);
});
};
}
8 changes: 4 additions & 4 deletions server/controllers/productions/update.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -15,4 +15,4 @@ module.exports = (req, res, next) => {
if (err) return next(err);
res.redirect(`/productions/${id}`);
});
};
}
5 changes: 2 additions & 3 deletions server/models/database.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
23 changes: 12 additions & 11 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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 };
3 changes: 3 additions & 0 deletions setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('babel-register')({
plugins: ["transform-es2015-modules-commonjs"]
});