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

moved to connect-rest 2.0 #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 14 additions & 25 deletions ch15/meadowlark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')){
Expand All @@ -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'],
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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 )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why let here and var above? I'm all about const these days, but this book was written before it was easy to do all of that without transcompilation. This will all be pretty significantly different in the 2nd edition. Also, I'm not fond of unnecessary identifiers...other than calling create, you don't use Rest anywhere else. So I would write this as:

const rest = require('connect-rest').create({ context: '' })


rest.get('/attractions', function(req, content, cb){
Attraction.find({ approved: true }, function(err, attractions){
Expand Down Expand Up @@ -271,45 +277,28 @@ 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,
});
});
});

// 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
Expand Down
2 changes: 1 addition & 1 deletion ch15/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 16 additions & 25 deletions ch16/meadowlark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')){
Expand All @@ -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'],
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -294,46 +302,29 @@ 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,
});
});
});

// 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
Expand Down
2 changes: 1 addition & 1 deletion ch16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 14 additions & 25 deletions ch17/meadowlark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')){
Expand All @@ -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'],
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -294,46 +300,29 @@ 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,
});
});
});

// 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
Expand Down
2 changes: 1 addition & 1 deletion ch17/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading