Skip to content

Commit 60416ab

Browse files
committed
update to Express 4
Ref gruntjs#128
1 parent f299ef6 commit 60416ab

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
},
1919
"private": true,
2020
"dependencies": {
21-
"express": "~3.4.8",
21+
"express": "~4.9.1",
22+
"body-parser": "~1.8.2",
23+
"compression": "~1.1.0",
24+
"errorhandler": "~1.2.0",
25+
"method-override": "~2.2.0",
2226
"node-schedule": "~0.1.13",
2327
"grunt": "~0.4.2",
2428
"grunt-cli": "~0.1.x",

server.js

+44-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
var express = require('express');
2+
var compression = require('compression');
3+
var bodyParser = require('body-parser');
4+
var methodOverride = require('method-override');
5+
var errorHandler = require('errorhandler');
26
var app = express();
37
var fs = require('fs');
48
var path = require('path');
@@ -20,37 +24,22 @@ app.enable('strict routing');
2024
/**
2125
* express app configuration
2226
*/
23-
app.configure(function () {
24-
app.use(express.compress());
25-
app.use(express.methodOverride());
26-
app.use(express.bodyParser());
27-
app.set('views', path.join(__dirname, 'src', 'tmpl'));
28-
app.set('view engine', 'jade');
29-
30-
// strip slashes
31-
app.use(function (req, res, next) {
32-
if (req.url.substr(-1) === '/' && req.url.length > 1) {
33-
res.redirect(301, req.url.slice(0, -1));
34-
} else {
35-
next();
36-
}
37-
});
38-
// use the router
39-
app.use(app.router);
40-
// use the static router
41-
app.use(express.static('build'));
42-
// if nothing matched, send 404
43-
app.use(function (req, res) {
44-
res.status(404).render('404', {
45-
page: 'notfound',
46-
title: '404 Not Found'
47-
});
48-
});
49-
app.use(express.errorHandler({
50-
dumpExceptions:false,
51-
showStack:false
52-
}));
53-
27+
app.use(compression());
28+
app.use(methodOverride());
29+
app.use(bodyParser.urlencoded({
30+
extended: true
31+
}));
32+
app.use(bodyParser.json());
33+
app.set('views', path.join(__dirname, 'src', 'tmpl'));
34+
app.set('view engine', 'jade');
35+
36+
// strip slashes
37+
app.use(function (req, res, next) {
38+
if (req.url.substr(-1) === '/' && req.url.length > 1) {
39+
res.redirect(301, req.url.slice(0, -1));
40+
} else {
41+
next();
42+
}
5443
});
5544

5645
/**
@@ -71,7 +60,7 @@ app.get('/', function (req, res, next) {
7160

7261
fs.exists(filePath, function (exists) {
7362
if (exists) {
74-
res.sendfile(filePath);
63+
res.sendFile(filePath, { root: __dirname });
7564
} else {
7665
next();
7766
}
@@ -91,7 +80,7 @@ app.get('/api*', function (req, res, next) {
9180

9281
fs.exists(filePath, function (exists) {
9382
if (exists) {
94-
res.sendfile(filePath);
83+
res.sendFile(filePath, { root: __dirname });
9584
} else {
9685
next();
9786
}
@@ -104,12 +93,12 @@ app.get('/blog*', function (req, res, next) {
10493
var filePath = 'build' + cleanUrl + '.html';
10594

10695
if (cleanUrl === '/blog') {
107-
res.sendfile('build/blog.html');
96+
res.sendFile('build/blog.html', { root: __dirname });
10897
} else {
10998

11099
fs.exists(filePath, function (exists) {
111100
if (exists) {
112-
res.sendfile(filePath);
101+
res.sendFile(filePath, { root: __dirname });
113102
} else {
114103
next();
115104
}
@@ -119,14 +108,14 @@ app.get('/blog*', function (req, res, next) {
119108

120109
// plugins route
121110
app.get('/plugins*', function (req, res) {
122-
res.sendfile('build/plugins.html');
111+
res.sendFile('build/plugins.html', { root: __dirname });
123112
});
124113

125114
// rss atom feed
126115
app.get('/rss', function (req, res) {
127116
res.setHeader('Content-Type', 'application/xml');
128117
res.setHeader('Charset', 'utf-8');
129-
res.sendfile('build/atom.xml');
118+
res.sendFile('build/atom.xml', { root: __dirname });
130119
});
131120

132121
// final route, if nothing else matched, this will match docs
@@ -141,10 +130,27 @@ app.get('*', function (req, res, next) {
141130

142131
fs.exists(filePath, function (exists) {
143132
if (exists) {
144-
res.sendfile(filePath);
133+
res.sendFile(filePath, { root: __dirname });
145134
} else {
146135
next();
147136
}
148137
});
149138

150139
});
140+
141+
/**
142+
* express app configuration after routes
143+
*/
144+
// use the static router
145+
app.use(express.static('build'));
146+
// if nothing matched, send 404
147+
app.use(function (req, res) {
148+
res.status(404).render('404', {
149+
page: 'notfound',
150+
title: '404 Not Found'
151+
});
152+
});
153+
app.use(errorHandler({
154+
dumpExceptions:false,
155+
showStack:false
156+
}));

0 commit comments

Comments
 (0)