1
1
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' ) ;
2
6
var app = express ( ) ;
3
7
var fs = require ( 'fs' ) ;
4
8
var path = require ( 'path' ) ;
@@ -20,37 +24,22 @@ app.enable('strict routing');
20
24
/**
21
25
* express app configuration
22
26
*/
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
+ }
54
43
} ) ;
55
44
56
45
/**
@@ -71,7 +60,7 @@ app.get('/', function (req, res, next) {
71
60
72
61
fs . exists ( filePath , function ( exists ) {
73
62
if ( exists ) {
74
- res . sendfile ( filePath ) ;
63
+ res . sendFile ( filePath , { root : __dirname } ) ;
75
64
} else {
76
65
next ( ) ;
77
66
}
@@ -91,7 +80,7 @@ app.get('/api*', function (req, res, next) {
91
80
92
81
fs . exists ( filePath , function ( exists ) {
93
82
if ( exists ) {
94
- res . sendfile ( filePath ) ;
83
+ res . sendFile ( filePath , { root : __dirname } ) ;
95
84
} else {
96
85
next ( ) ;
97
86
}
@@ -104,12 +93,12 @@ app.get('/blog*', function (req, res, next) {
104
93
var filePath = 'build' + cleanUrl + '.html' ;
105
94
106
95
if ( cleanUrl === '/blog' ) {
107
- res . sendfile ( 'build/blog.html' ) ;
96
+ res . sendFile ( 'build/blog.html' , { root : __dirname } ) ;
108
97
} else {
109
98
110
99
fs . exists ( filePath , function ( exists ) {
111
100
if ( exists ) {
112
- res . sendfile ( filePath ) ;
101
+ res . sendFile ( filePath , { root : __dirname } ) ;
113
102
} else {
114
103
next ( ) ;
115
104
}
@@ -119,14 +108,14 @@ app.get('/blog*', function (req, res, next) {
119
108
120
109
// plugins route
121
110
app . get ( '/plugins*' , function ( req , res ) {
122
- res . sendfile ( 'build/plugins.html' ) ;
111
+ res . sendFile ( 'build/plugins.html' , { root : __dirname } ) ;
123
112
} ) ;
124
113
125
114
// rss atom feed
126
115
app . get ( '/rss' , function ( req , res ) {
127
116
res . setHeader ( 'Content-Type' , 'application/xml' ) ;
128
117
res . setHeader ( 'Charset' , 'utf-8' ) ;
129
- res . sendfile ( 'build/atom.xml' ) ;
118
+ res . sendFile ( 'build/atom.xml' , { root : __dirname } ) ;
130
119
} ) ;
131
120
132
121
// final route, if nothing else matched, this will match docs
@@ -141,10 +130,27 @@ app.get('*', function (req, res, next) {
141
130
142
131
fs . exists ( filePath , function ( exists ) {
143
132
if ( exists ) {
144
- res . sendfile ( filePath ) ;
133
+ res . sendFile ( filePath , { root : __dirname } ) ;
145
134
} else {
146
135
next ( ) ;
147
136
}
148
137
} ) ;
149
138
150
139
} ) ;
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