Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit c9d641e

Browse files
author
Marc-André Bélanger
committed
Migration to Express 4.x
Please see the Express documentation for details. connect-mongo NPM package is currently broken with Express 4.x. Use this fix in the meantime: jdesboeufs/connect-mongo#103 (comment) 50 .
1 parent f150407 commit c9d641e

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

app/routes/articles.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ var users = require('../../app/controllers/users'),
77
articles = require('../../app/controllers/articles');
88

99
module.exports = function(app) {
10-
// Article Routes
11-
app.get('/articles', articles.list);
12-
app.post('/articles', users.requiresLogin, articles.create);
13-
app.get('/articles/:articleId', articles.read);
14-
app.put('/articles/:articleId', users.requiresLogin, articles.hasAuthorization, articles.update);
10+
// Article Routes, using express 4.x syntax
11+
app.route('/articles')
12+
.get(articles.list)
13+
.post(users.requiresLogin, articles.create);
14+
15+
app.route('/articles/:articleId')
16+
.get(articles.read)
17+
.put(users.requiresLogin, articles.hasAuthorization, articles.update);
1518
app.del('/articles/:articleId', users.requiresLogin, articles.hasAuthorization, articles.delete);
1619

1720
// Finish by binding the article middleware

app/routes/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
module.exports = function(app) {
44
// Root routing
55
var core = require('../../app/controllers/core');
6-
app.get('/', core.index);
6+
app.route('/')
7+
.get(core.index);
78
};

config/express.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
* Module dependencies.
55
*/
66
var express = require('express'),
7+
morgan = require('morgan'),
8+
bodyParser = require('body-parser'),
9+
session = require('express-session'),
10+
compress = require('compression'),
11+
methodOverride = require('method-override'),
12+
cookieParser = require('cookie-parser'),
713
passport = require('passport'),
8-
mongoStore = require('connect-mongo')(express),
14+
mongoStore = require('connect-mongo')(session),
915
flash = require('connect-flash'),
1016
config = require('./config'),
1117
consolidate = require('consolidate'),
@@ -22,14 +28,12 @@ module.exports = function(db) {
2228
});
2329

2430
// Setting the environment locals
25-
app.locals({
26-
title: config.app.title,
27-
description: config.app.description,
28-
keywords: config.app.keywords,
29-
facebookAppId: config.facebook.clientID,
30-
modulesJSFiles: utilities.walk('./public/modules', /(.*)\.(js)/, /(.*)\.(spec.js)/, './public'),
31-
modulesCSSFiles: utilities.walk('./public/modules', /(.*)\.(css)/, null, './public')
32-
});
31+
app.locals.title = config.app.title;
32+
app.locals.description = config.app.description;
33+
app.locals.keywords = config.app.keywords;
34+
app.locals.facebookAppId = config.facebook.clientID;
35+
app.locals.modulesJSFiles = utilities.walk('./public/modules', /(.*)\.(js)/, /(.*)\.(spec.js)/, './public');
36+
app.locals.modulesCSSFiles = utilities.walk('./public/modules', /(.*)\.(css)/, null, './public');
3337

3438
// Passing the request url to environment locals
3539
app.use(function(req, res, next) {
@@ -38,7 +42,7 @@ module.exports = function(db) {
3842
});
3943

4044
// Should be placed before express.static
41-
app.use(express.compress({
45+
app.use(compress({
4246
filter: function(req, res) {
4347
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
4448
},
@@ -55,35 +59,33 @@ module.exports = function(db) {
5559
app.set('view engine', 'html');
5660
app.set('views', config.root + '/app/views');
5761

58-
// Application Configuration for development environment
59-
app.configure('development', function() {
60-
// Enable logger
61-
app.use(express.logger('dev'));
62+
// Environment dependent middleware
63+
if (process.env.NODE_ENV === 'development'){
64+
// Enable logger (morgan)
65+
app.use(morgan('dev'));
6266

6367
// Disable views cache
6468
app.set('view cache', false);
65-
});
66-
67-
// Application Configuration for production environment
68-
app.configure('production', function() {
69+
} else if (process.env.NODE_ENV === 'production'){
6970
app.locals({
7071
cache: 'memory' // To solve SWIG Cache Issues
7172
});
72-
});
73+
}
74+
7375

7476
// request body parsing middleware should be above methodOverride
75-
app.use(express.urlencoded());
76-
app.use(express.json());
77-
app.use(express.methodOverride());
77+
app.use(bodyParser.urlencoded());
78+
app.use(bodyParser.json());
79+
app.use(methodOverride());
7880

7981
// Enable jsonp
8082
app.enable('jsonp callback');
8183

8284
// cookieParser should be above session
83-
app.use(express.cookieParser());
85+
app.use(cookieParser());
8486

8587
// express/mongo session storage
86-
app.use(express.session({
88+
app.use(session({
8789
secret: config.sessionSecret,
8890
store: new mongoStore({
8991
db: db.connection.db,
@@ -98,9 +100,6 @@ module.exports = function(db) {
98100
// connect flash for flash messages
99101
app.use(flash());
100102

101-
// routes should be at the last
102-
app.use(app.router);
103-
104103
// Setting the app router and static folder
105104
app.use(express.static(config.root + '/public'));
106105

package.json

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,65 @@
11
{
2-
"name": "meanjs",
3-
"description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js.",
4-
"version": "0.2.2",
5-
"private": false,
6-
"author": "https://github.com/meanjs/mean/graphs/contributors",
7-
"repository": {
8-
"type": "git",
9-
"url": "https://github.com/meanjs/mean.git"
10-
},
11-
"engines": {
12-
"node": "0.10.x",
13-
"npm": "1.4.x"
14-
},
15-
"scripts": {
16-
"start": "grunt",
17-
"test": "grunt test",
18-
"postinstall": "bower install --config.interactive=false"
19-
},
20-
"dependencies": {
21-
"express": "~3.5.1",
22-
"consolidate": "~0.10.0",
23-
"swig": "~1.3.2",
24-
"mongoose": "~3.8.8",
25-
"connect-mongo": "~0.4.0",
26-
"connect-flash": "~0.1.1",
27-
"passport": "~0.2.0",
28-
"passport-local": "~1.0.0",
29-
"passport-facebook": "~1.0.2",
30-
"passport-twitter": "~1.0.2",
31-
"passport-linkedin": "~0.1.3",
32-
"passport-google-oauth": "~0.1.5",
33-
"lodash": "~2.4.1",
34-
"forever": "~0.10.11",
35-
"bower": "~1.3.1",
36-
"grunt-cli": "~0.1.13"
37-
},
38-
"devDependencies": {
39-
"supertest": "~0.10.0",
40-
"should": "~3.2.0",
41-
"grunt-env": "~0.4.1",
42-
"grunt-node-inspector": "~0.1.3",
43-
"grunt-contrib-watch": "~0.6.1",
44-
"grunt-contrib-jshint": "~0.10.0",
45-
"grunt-nodemon": "~0.2.0",
46-
"grunt-concurrent": "~0.5.0",
47-
"grunt-mocha-test": "~0.10.0",
48-
"grunt-karma": "~0.8.2",
49-
"karma": "~0.12.0",
50-
"karma-jasmine": "~0.2.1",
51-
"karma-coverage": "~0.2.0",
52-
"karma-chrome-launcher": "~0.1.2",
53-
"karma-firefox-launcher": "~0.1.3",
54-
"karma-phantomjs-launcher": "~0.1.2"
55-
}
2+
"name": "meanjs",
3+
"description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js.",
4+
"version": "0.2.3",
5+
"private": false,
6+
"author": "https://github.com/meanjs/mean/graphs/contributors",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/meanjs/mean.git"
10+
},
11+
"engines": {
12+
"node": "0.10.x",
13+
"npm": "1.4.x"
14+
},
15+
"scripts": {
16+
"start": "grunt",
17+
"test": "grunt test",
18+
"postinstall": "bower install --config.interactive=false"
19+
},
20+
"dependencies": {
21+
"body-parser": "^1.0.1",
22+
"bower": "~1.3.1",
23+
"compression": "^1.0.1",
24+
"connect-flash": "~0.1.1",
25+
"connect-mongo": "mrzepinski/connect-mongo#2135988",
26+
"consolidate": "~0.10.0",
27+
"cookie-parser": "^1.0.1",
28+
"express": "4.x",
29+
"express-session": "^1.0.2",
30+
"forever": "~0.10.11",
31+
"grunt-cli": "~0.1.13",
32+
"grunt-mocha-test": "^0.10.2",
33+
"karma-jasmine": "^0.2.2",
34+
"lodash": "~2.4.1",
35+
"method-override": "^1.0.0",
36+
"mongoose": "~3.8.8",
37+
"morgan": "^1.0.0",
38+
"passport": "~0.2.0",
39+
"passport-facebook": "~1.0.2",
40+
"passport-google-oauth": "~0.1.5",
41+
"passport-linkedin": "~0.1.3",
42+
"passport-local": "~1.0.0",
43+
"passport-twitter": "~1.0.2",
44+
"should": "^3.2.0",
45+
"swig": "~1.3.2"
46+
},
47+
"devDependencies": {
48+
"supertest": "~0.10.0",
49+
"should": "~3.2.0",
50+
"grunt-env": "~0.4.1",
51+
"grunt-node-inspector": "~0.1.3",
52+
"grunt-contrib-watch": "~0.6.1",
53+
"grunt-contrib-jshint": "~0.10.0",
54+
"grunt-nodemon": "~0.2.0",
55+
"grunt-concurrent": "~0.5.0",
56+
"grunt-mocha-test": "~0.10.0",
57+
"grunt-karma": "~0.8.2",
58+
"karma": "~0.12.0",
59+
"karma-jasmine": "~0.2.1",
60+
"karma-coverage": "~0.2.0",
61+
"karma-chrome-launcher": "~0.1.2",
62+
"karma-firefox-launcher": "~0.1.3",
63+
"karma-phantomjs-launcher": "~0.1.2"
64+
}
5665
}

0 commit comments

Comments
 (0)