From 2dc3acc0823b9084201ee59a52d89dbc0bed6595 Mon Sep 17 00:00:00 2001 From: Imre Fazekas Date: Sat, 1 Oct 2016 09:33:49 +0200 Subject: [PATCH] moved to connect-rest 2.0 --- ch15/meadowlark.js | 39 ++++++++++++++------------------------- ch15/package.json | 2 +- ch16/meadowlark.js | 41 ++++++++++++++++------------------------- ch16/package.json | 2 +- ch17/meadowlark.js | 39 ++++++++++++++------------------------- ch17/package.json | 2 +- ch18/meadowlark.js | 39 ++++++++++++++------------------------- ch18/package.json | 2 +- ch19/meadowlark.js | 41 +++++++++++++++-------------------------- ch19/package.json | 2 +- 10 files changed, 78 insertions(+), 131 deletions(-) diff --git a/ch15/meadowlark.js b/ch15/meadowlark.js index f1a17aa..0ed8afe 100644 --- a/ch15/meadowlark.js +++ b/ch15/meadowlark.js @@ -103,7 +103,7 @@ app.use(require('body-parser')()); var mongoose = require('mongoose'); var options = { server: { - socketOptions: { keepAlive: 1 } + socketOptions: { keepAlive: 1 } } }; switch(app.get('env')){ @@ -126,7 +126,7 @@ Vacation.find(function(err, vacations){ slug: 'hood-river-day-trip', category: 'Day Trip', sku: 'HR199', - description: 'Spend a day sailing on the Columbia and ' + + description: 'Spend a day sailing on the Columbia and ' + 'enjoying craft beers in Hood River!', priceInCents: 9995, tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'], @@ -178,7 +178,7 @@ app.use(function(req, res, next){ // set 'showTests' context property if the querystring contains test=1 app.use(function(req, res, next){ - res.locals.showTests = app.get('env') !== 'production' && + res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; next(); }); @@ -241,7 +241,13 @@ require('./routes.js')(app); var Attraction = require('./models/attraction.js'); -var rest = require('connect-rest'); +// API configuration +var apiOptions = { + context: '' +}; + +var Rest = require('connect-rest'); +let rest = Rest.create( apiOptions ) rest.get('/attractions', function(req, content, cb){ Attraction.find({ approved: true }, function(err, attractions){ @@ -271,13 +277,13 @@ rest.post('/attraction', function(req, content, cb){ a.save(function(err, a){ if(err) return cb({ error: 'Unable to add attraction.' }); cb(null, { id: a._id }); - }); + }); }); rest.get('/attraction/:id', function(req, content, cb){ Attraction.findById(req.params.id, function(err, a){ if(err) return cb({ error: 'Unable to retrieve attraction.' }); - cb(null, { + cb(null, { name: a.name, description: a.description, location: a.location, @@ -285,31 +291,14 @@ rest.get('/attraction/:id', function(req, content, cb){ }); }); -// API configuration -var apiOptions = { - context: '/', - domain: require('domain').create(), -}; - -apiOptions.domain.on('error', function(err){ - console.log('API domain error.\n', err.stack); - setTimeout(function(){ - console.log('Server shutting down after API domain error.'); - process.exit(1); - }, 5000); - server.close(); - var worker = require('cluster').worker; - if(worker) worker.disconnect(); -}); - // link API into pipeline -app.use(vhost('api.*', rest.rester(apiOptions))); +app.use(vhost('api.*', rest.processRequest()) ) // add support for auto views var autoViews = {}; app.use(function(req,res,next){ - var path = req.path.toLowerCase(); + var path = req.path.toLowerCase(); // check cache; if it's there, render the view if(autoViews[path]) return res.render(autoViews[path]); // if it's not in the cache, see if there's diff --git a/ch15/package.json b/ch15/package.json index fe8c7a6..24bf68c 100644 --- a/ch15/package.json +++ b/ch15/package.json @@ -20,7 +20,7 @@ "body-parser": "~1.3.0", "chai": "~1.9.1", "connect": "^2.19.5", - "connect-rest": "^0.9.12", + "connect-rest": "^2.9.1", "cookie-parser": "^1.1.0", "cors": "^2.3.1", "express": "~4.4.2", diff --git a/ch16/meadowlark.js b/ch16/meadowlark.js index 78f9f71..bc06125 100644 --- a/ch16/meadowlark.js +++ b/ch16/meadowlark.js @@ -110,7 +110,7 @@ app.use(require('body-parser')()); var mongoose = require('mongoose'); var options = { server: { - socketOptions: { keepAlive: 1 } + socketOptions: { keepAlive: 1 } } }; switch(app.get('env')){ @@ -133,7 +133,7 @@ Vacation.find(function(err, vacations){ slug: 'hood-river-day-trip', category: 'Day Trip', sku: 'HR199', - description: 'Spend a day sailing on the Columbia and ' + + description: 'Spend a day sailing on the Columbia and ' + 'enjoying craft beers in Hood River!', priceInCents: 9995, tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'], @@ -185,7 +185,7 @@ app.use(function(req, res, next){ // set 'showTests' context property if the querystring contains test=1 app.use(function(req, res, next){ - res.locals.showTests = app.get('env') !== 'production' && + res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; next(); }); @@ -264,7 +264,15 @@ require('./routes.js')(app); var Attraction = require('./models/attraction.js'); -var rest = require('connect-rest'); + + +// API configuration +var apiOptions = { + context: '' +}; + +var Rest = require('connect-rest') +let rest = Rest.create( apiOptions ) rest.get('/attractions', function(req, content, cb){ Attraction.find({ approved: true }, function(err, attractions){ @@ -294,13 +302,13 @@ rest.post('/attraction', function(req, content, cb){ a.save(function(err, a){ if(err) return cb({ error: 'Unable to add attraction.' }); cb(null, { id: a._id }); - }); + }); }); rest.get('/attraction/:id', function(req, content, cb){ Attraction.findById(req.params.id, function(err, a){ if(err) return cb({ error: 'Unable to retrieve attraction.' }); - cb(null, { + cb(null, { name: a.name, description: a.description, location: a.location, @@ -308,32 +316,15 @@ rest.get('/attraction/:id', function(req, content, cb){ }); }); -// API configuration -var apiOptions = { - context: '/', - domain: require('domain').create(), -}; - -apiOptions.domain.on('error', function(err){ - console.log('API domain error.\n', err.stack); - setTimeout(function(){ - console.log('Server shutting down after API domain error.'); - process.exit(1); - }, 5000); - server.close(); - var worker = require('cluster').worker; - if(worker) worker.disconnect(); -}); - // link API into pipeline // currently commented out to reduce console noise -//app.use(vhost('api.*', rest.rester(apiOptions))); +// app.use(vhost('api.*', rest.processRequest()) ) // add support for auto views var autoViews = {}; app.use(function(req,res,next){ - var path = req.path.toLowerCase(); + var path = req.path.toLowerCase(); // check cache; if it's there, render the view if(autoViews[path]) return res.render(autoViews[path]); // if it's not in the cache, see if there's diff --git a/ch16/package.json b/ch16/package.json index ba67497..3214e8e 100644 --- a/ch16/package.json +++ b/ch16/package.json @@ -21,7 +21,7 @@ "chai": "~1.9.1", "connect": "^2.19.5", "connect-bundle": "0.0.5-beta", - "connect-rest": "^0.9.12", + "connect-rest": "^2.9.1", "cookie-parser": "^1.1.0", "cors": "^2.3.1", "express": "~4.4.2", diff --git a/ch17/meadowlark.js b/ch17/meadowlark.js index 78f9f71..6327046 100644 --- a/ch17/meadowlark.js +++ b/ch17/meadowlark.js @@ -110,7 +110,7 @@ app.use(require('body-parser')()); var mongoose = require('mongoose'); var options = { server: { - socketOptions: { keepAlive: 1 } + socketOptions: { keepAlive: 1 } } }; switch(app.get('env')){ @@ -133,7 +133,7 @@ Vacation.find(function(err, vacations){ slug: 'hood-river-day-trip', category: 'Day Trip', sku: 'HR199', - description: 'Spend a day sailing on the Columbia and ' + + description: 'Spend a day sailing on the Columbia and ' + 'enjoying craft beers in Hood River!', priceInCents: 9995, tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'], @@ -185,7 +185,7 @@ app.use(function(req, res, next){ // set 'showTests' context property if the querystring contains test=1 app.use(function(req, res, next){ - res.locals.showTests = app.get('env') !== 'production' && + res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; next(); }); @@ -264,7 +264,13 @@ require('./routes.js')(app); var Attraction = require('./models/attraction.js'); -var rest = require('connect-rest'); +// API configuration +var apiOptions = { + context: '' +}; + +var Rest = require('connect-rest') +let rest = Rest.create( apiOptions ) rest.get('/attractions', function(req, content, cb){ Attraction.find({ approved: true }, function(err, attractions){ @@ -294,13 +300,13 @@ rest.post('/attraction', function(req, content, cb){ a.save(function(err, a){ if(err) return cb({ error: 'Unable to add attraction.' }); cb(null, { id: a._id }); - }); + }); }); rest.get('/attraction/:id', function(req, content, cb){ Attraction.findById(req.params.id, function(err, a){ if(err) return cb({ error: 'Unable to retrieve attraction.' }); - cb(null, { + cb(null, { name: a.name, description: a.description, location: a.location, @@ -308,32 +314,15 @@ rest.get('/attraction/:id', function(req, content, cb){ }); }); -// API configuration -var apiOptions = { - context: '/', - domain: require('domain').create(), -}; - -apiOptions.domain.on('error', function(err){ - console.log('API domain error.\n', err.stack); - setTimeout(function(){ - console.log('Server shutting down after API domain error.'); - process.exit(1); - }, 5000); - server.close(); - var worker = require('cluster').worker; - if(worker) worker.disconnect(); -}); - // link API into pipeline // currently commented out to reduce console noise -//app.use(vhost('api.*', rest.rester(apiOptions))); +// app.use(vhost('api.*', rest.processRequest()) ) // add support for auto views var autoViews = {}; app.use(function(req,res,next){ - var path = req.path.toLowerCase(); + var path = req.path.toLowerCase(); // check cache; if it's there, render the view if(autoViews[path]) return res.render(autoViews[path]); // if it's not in the cache, see if there's diff --git a/ch17/package.json b/ch17/package.json index 198809f..3611139 100644 --- a/ch17/package.json +++ b/ch17/package.json @@ -21,7 +21,7 @@ "chai": "~1.9.1", "connect": "^2.19.5", "connect-bundle": "0.0.5-beta", - "connect-rest": "^0.9.12", + "connect-rest": "^2.9.1", "cookie-parser": "^1.1.0", "cors": "^2.3.1", "express": "~4.4.2", diff --git a/ch18/meadowlark.js b/ch18/meadowlark.js index b331683..7cc20f0 100644 --- a/ch18/meadowlark.js +++ b/ch18/meadowlark.js @@ -116,7 +116,7 @@ app.use(express.static(__dirname + '/public')); var mongoose = require('mongoose'); var options = { server: { - socketOptions: { keepAlive: 1 } + socketOptions: { keepAlive: 1 } } }; switch(app.get('env')){ @@ -139,7 +139,7 @@ Vacation.find(function(err, vacations){ slug: 'hood-river-day-trip', category: 'Day Trip', sku: 'HR199', - description: 'Spend a day sailing on the Columbia and ' + + description: 'Spend a day sailing on the Columbia and ' + 'enjoying craft beers in Hood River!', priceInCents: 9995, tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'], @@ -191,7 +191,7 @@ app.use(function(req, res, next){ // set 'showTests' context property if the querystring contains test=1 app.use(function(req, res, next){ - res.locals.showTests = app.get('env') !== 'production' && + res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; next(); }); @@ -270,7 +270,13 @@ require('./routes.js')(app); var Attraction = require('./models/attraction.js'); -var rest = require('connect-rest'); +// API configuration +var apiOptions = { + context: '' +}; + +var Rest = require('connect-rest') +let rest = Rest.create( apiOptions ) rest.get('/attractions', function(req, content, cb){ Attraction.find({ approved: true }, function(err, attractions){ @@ -300,13 +306,13 @@ rest.post('/attraction', function(req, content, cb){ a.save(function(err, a){ if(err) return cb({ error: 'Unable to add attraction.' }); cb(null, { id: a._id }); - }); + }); }); rest.get('/attraction/:id', function(req, content, cb){ Attraction.findById(req.params.id, function(err, a){ if(err) return cb({ error: 'Unable to retrieve attraction.' }); - cb(null, { + cb(null, { name: a.name, description: a.description, location: a.location, @@ -314,26 +320,9 @@ rest.get('/attraction/:id', function(req, content, cb){ }); }); -// API configuration -var apiOptions = { - context: '/', - domain: require('domain').create(), -}; - -apiOptions.domain.on('error', function(err){ - console.log('API domain error.\n', err.stack); - setTimeout(function(){ - console.log('Server shutting down after API domain error.'); - process.exit(1); - }, 5000); - server.close(); - var worker = require('cluster').worker; - if(worker) worker.disconnect(); -}); - // link API into pipeline // currently commented out to reduce console noise -//app.use(vhost('api.*', rest.rester(apiOptions))); +// app.use(vhost('api.*', rest.processRequest()) ) // authentication var auth = require('./lib/auth.js')(app, { @@ -392,7 +381,7 @@ app.get('/sales', employeeOnly, function(req, res){ var autoViews = {}; app.use(function(req,res,next){ - var path = req.path.toLowerCase(); + var path = req.path.toLowerCase(); // check cache; if it's there, render the view if(autoViews[path]) return res.render(autoViews[path]); // if it's not in the cache, see if there's diff --git a/ch18/package.json b/ch18/package.json index 0b12172..994ad7d 100644 --- a/ch18/package.json +++ b/ch18/package.json @@ -21,7 +21,7 @@ "chai": "~1.9.1", "connect": "^2.19.5", "connect-bundle": "0.0.5-beta", - "connect-rest": "^0.9.12", + "connect-rest": "^2.9.1", "cookie-parser": "^1.1.0", "cors": "^2.3.1", "csurf": "^1.8.3", diff --git a/ch19/meadowlark.js b/ch19/meadowlark.js index b50b1f3..068b5b7 100644 --- a/ch19/meadowlark.js +++ b/ch19/meadowlark.js @@ -125,7 +125,7 @@ app.use(express.static(__dirname + '/public')); var mongoose = require('mongoose'); var options = { server: { - socketOptions: { keepAlive: 1 } + socketOptions: { keepAlive: 1 } } }; switch(app.get('env')){ @@ -148,7 +148,7 @@ Vacation.find(function(err, vacations){ slug: 'hood-river-day-trip', category: 'Day Trip', sku: 'HR199', - description: 'Spend a day sailing on the Columbia and ' + + description: 'Spend a day sailing on the Columbia and ' + 'enjoying craft beers in Hood River!', priceInCents: 9995, tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'], @@ -192,7 +192,7 @@ Vacation.find(function(err, vacations){ // initialize dealers Dealer.find({}, function(err, dealers){ if(dealers.length) return; - + new Dealer({ name: 'Oregon Novelties', address1: '912 NW Davis St', @@ -351,7 +351,7 @@ app.use(function(req, res, next){ // set 'showTests' context property if the querystring contains test=1 app.use(function(req, res, next){ - res.locals.showTests = app.get('env') !== 'production' && + res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1'; next(); }); @@ -488,7 +488,13 @@ require('./routes.js')(app); var Attraction = require('./models/attraction.js'); -var rest = require('connect-rest'); +// API configuration +var apiOptions = { + context: '' +}; + +var Rest = require('connect-rest') +let rest = Rest.create( apiOptions ) rest.get('/attractions', function(req, content, cb){ Attraction.find({ approved: true }, function(err, attractions){ @@ -518,13 +524,13 @@ rest.post('/attraction', function(req, content, cb){ a.save(function(err, a){ if(err) return cb({ error: 'Unable to add attraction.' }); cb(null, { id: a._id }); - }); + }); }); rest.get('/attraction/:id', function(req, content, cb){ Attraction.findById(req.params.id, function(err, a){ if(err) return cb({ error: 'Unable to retrieve attraction.' }); - cb(null, { + cb(null, { name: a.name, description: a.description, location: a.location, @@ -532,26 +538,9 @@ rest.get('/attraction/:id', function(req, content, cb){ }); }); -// API configuration -var apiOptions = { - context: '/', - domain: require('domain').create(), -}; - -apiOptions.domain.on('error', function(err){ - console.log('API domain error.\n', err.stack); - setTimeout(function(){ - console.log('Server shutting down after API domain error.'); - process.exit(1); - }, 5000); - server.close(); - var worker = require('cluster').worker; - if(worker) worker.disconnect(); -}); - // link API into pipeline // currently commented out to reduce console noise -//app.use(vhost('api.*', rest.rester(apiOptions))); +// app.use(vhost('api.*', rest.processRequest()) ) // authentication var auth = require('./lib/auth.js')(app, { @@ -610,7 +599,7 @@ app.get('/sales', employeeOnly, function(req, res){ var autoViews = {}; app.use(function(req,res,next){ - var path = req.path.toLowerCase(); + var path = req.path.toLowerCase(); // check cache; if it's there, render the view if(autoViews[path]) return res.render(autoViews[path]); // if it's not in the cache, see if there's diff --git a/ch19/package.json b/ch19/package.json index 6ad8981..3b62155 100644 --- a/ch19/package.json +++ b/ch19/package.json @@ -21,7 +21,7 @@ "chai": "~1.9.1", "connect": "^2.19.5", "connect-bundle": "0.0.5-beta", - "connect-rest": "^0.9.12", + "connect-rest": "^2.9.1", "cookie-parser": "^1.1.0", "cors": "^2.3.1", "csurf": "^1.8.3",