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

Upgrade to es6 #85

Merged
merged 4 commits into from
Oct 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions 1-hello-world/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@

'use strict';

var express = require('express');
const express = require('express');

var app = express();
const app = express();

// [START hello_world]
// Say hello!
app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.status(200).send('Hello, world!');
});
// [END hello_world]

if (module === require.main) {
// [START server]
// Start the server
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log('App listening on port %s', port);
const server = app.listen(process.env.PORT || 8080, () => {
const port = server.address().port;
console.log(`App listening on port ${port}`);
});
// [END server]
}
Expand Down
2 changes: 1 addition & 1 deletion 1-hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
"supertest": "^2.0.0"
},
"engines": {
"node": ">=0.12.7"
"node": ">=4.3.2"
}
}
16 changes: 8 additions & 8 deletions 1-hello-world/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

'use strict';

var assert = require('assert');
var config = require('./config');
var utils = require('nodejs-repo-tools');
const assert = require(`assert`);
const config = require(`./config`);
const utils = require(`nodejs-repo-tools`);

describe('app.js', function () {
describe(`app.js`, () => {
if (!process.env.E2E_TESTS) {
it('should run', function (done) {
it(`should run`, (done) => {
utils.testLocalApp(config, done);
});
}

it('should create an express app', function (done) {
it(`should create an express app`, (done) => {
utils.getRequest(config)
.get('/')
.get(`/`)
.expect(200)
.expect(function (response) {
.expect((response) => {
assert.equal(response.text, config.msg);
})
.end(done);
Expand Down
10 changes: 5 additions & 5 deletions 1-hello-world/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

'use strict';

var path = require('path');
const path = require(`path`);

module.exports = {
test: '1-hello-world',
test: `1-hello-world`,
cwd: path.resolve(path.join(__dirname, '../')),
cmd: 'node',
args: ['app.js'],
msg: 'Hello, world!'
cmd: `node`,
args: [`app.js`],
msg: `Hello, world!`
};
13 changes: 6 additions & 7 deletions 1-hello-world/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

'use strict';

var config = require('./config');
var utils = require('nodejs-repo-tools');
const config = require(`./config`);
const utils = require(`nodejs-repo-tools`);

describe(config.test + '/', function () {
describe(`${config.test}/`, () => {
if (!process.env.E2E_TESTS) {
it('should install dependencies', function (done) {
this.timeout(120 * 1000); // Allow 2 minutes to test installation
it(`should install dependencies`, (done) => {
utils.testInstallation(config, done);
});
}).timeout(120 * 1000);
}
require('./app.test');
require(`./app.test`);
});
20 changes: 10 additions & 10 deletions 2-structured-data/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

'use strict';

var path = require('path');
var express = require('express');
var config = require('./config');
const path = require('path');
const express = require('express');
const config = require('./config');

var app = express();
const app = express();

app.disable('etag');
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -29,17 +29,17 @@ app.use('/books', require('./books/crud'));
app.use('/api/books', require('./books/api'));

// Redirect root to /books
app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.redirect('/books');
});

// Basic 404 handler
app.use(function (req, res) {
app.use((req, res) => {
res.status(404).send('Not Found');
});

// Basic error handler
app.use(function (err, req, res, next) {
app.use((err, req, res, next) => {
/* jshint unused:false */
console.error(err);
// If our routes specified a specific response, then send that. Otherwise,
Expand All @@ -49,9 +49,9 @@ app.use(function (err, req, res, next) {

if (module === require.main) {
// Start the server
var server = app.listen(config.get('PORT'), function () {
var port = server.address().port;
console.log('App listening on port %s', port);
const server = app.listen(config.get('PORT'), () => {
const port = server.address().port;
console.log(`App listening on port ${port}`);
});
}

Expand Down
47 changes: 26 additions & 21 deletions 2-structured-data/books/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

'use strict';

var express = require('express');
var bodyParser = require('body-parser');
var config = require('../config');
const express = require('express');
const bodyParser = require('body-parser');
const config = require('../config');

function getModel () {
return require('./model-' + config.get('DATA_BACKEND'));
return require(`./model-${config.get('DATA_BACKEND')}`);
}

var router = express.Router();
const router = express.Router();

// Automatically parse request body as JSON
router.use(bodyParser.json());
Expand All @@ -31,10 +31,11 @@ router.use(bodyParser.json());
*
* Retrieve a page of books (up to ten at a time).
*/
router.get('/', function list (req, res, next) {
getModel().list(10, req.query.pageToken, function (err, entities, cursor) {
router.get('/', (req, res, next) => {
getModel().list(10, req.query.pageToken, (err, entities, cursor) => {
if (err) {
return next(err);
next(err);
return;
}
res.json({
items: entities,
Expand All @@ -48,10 +49,11 @@ router.get('/', function list (req, res, next) {
*
* Create a new book.
*/
router.post('/', function insert (req, res, next) {
getModel().create(req.body, function (err, entity) {
router.post('/', (req, res, next) => {
getModel().create(req.body, (err, entity) => {
if (err) {
return next(err);
next(err);
return;
}
res.json(entity);
});
Expand All @@ -62,10 +64,11 @@ router.post('/', function insert (req, res, next) {
*
* Retrieve a book.
*/
router.get('/:book', function get (req, res, next) {
getModel().read(req.params.book, function (err, entity) {
router.get('/:book', (req, res, next) => {
getModel().read(req.params.book, (err, entity) => {
if (err) {
return next(err);
next(err);
return;
}
res.json(entity);
});
Expand All @@ -76,10 +79,11 @@ router.get('/:book', function get (req, res, next) {
*
* Update a book.
*/
router.put('/:book', function update (req, res, next) {
getModel().update(req.params.book, req.body, function (err, entity) {
router.put('/:book', (req, res, next) => {
getModel().update(req.params.book, req.body, (err, entity) => {
if (err) {
return next(err);
next(err);
return;
}
res.json(entity);
});
Expand All @@ -90,10 +94,11 @@ router.put('/:book', function update (req, res, next) {
*
* Delete a book.
*/
router.delete('/:book', function _delete (req, res, next) {
getModel().delete(req.params.book, function (err) {
router.delete('/:book', (req, res, next) => {
getModel().delete(req.params.book, (err) => {
if (err) {
return next(err);
next(err);
return;
}
res.status(200).send('OK');
});
Expand All @@ -102,7 +107,7 @@ router.delete('/:book', function _delete (req, res, next) {
/**
* Errors on "/api/books/*" routes.
*/
router.use(function handleRpcError (err, req, res, next) {
router.use((err, req, res, next) => {
// Format error and forward to generic error handler for logging and
// responding to the request
err.response = {
Expand Down