initial commit
|
1 |
var express = require('express'); |
2 |
var path = require('path'); |
|
3 |
var favicon = require('static-favicon'); |
|
4 |
var logger = require('morgan'); |
|
5 |
var cookieParser = require('cookie-parser'); |
|
6 |
var bodyParser = require('body-parser'); |
|
7 | ||
8 |
var routes = require('./routes/index'); |
|
9 |
var users = require('./routes/users'); |
|
10 | ||
11 |
var app = express('8080', '127.0.0.1'); |
|
12 | ||
13 |
// view engine setup |
|
14 |
app.set('views', path.join(__dirname, 'views')); |
|
15 |
app.set('view engine', 'jade'); |
|
16 | ||
17 |
app.use(favicon()); |
|
18 |
app.use(logger('dev')); |
|
19 |
app.use(bodyParser.json()); |
|
20 |
app.use(bodyParser.urlencoded()); |
|
21 |
app.use(cookieParser()); |
|
22 |
app.use(express.static(path.join(__dirname, 'public'))); |
|
23 | ||
24 |
app.use('/', routes); |
|
25 |
app.use('/users', users); |
|
26 | ||
27 |
/// catch 404 and forwarding to error handler |
|
28 |
app.use(function(req, res, next) { |
|
29 |
console.log(req); |
|
30 |
var err = new Error('Not Found'); |
|
31 |
err.status = 404; |
|
32 |
next(err); |
|
33 |
}); |
|
34 | ||
35 |
/// error handlers |
|
36 | ||
37 |
// development error handler |
|
38 |
// will print stacktrace |
|
39 |
if (app.get('env') === 'development') { |
|
40 |
app.use(function(err, req, res, next) { |
|
41 |
res.status(err.status || 500); |
|
42 |
res.render('error', { |
|
43 |
message: err.message, |
|
44 |
error: err |
|
45 |
}); |
|
46 |
}); |
|
47 |
} |
|
48 | ||
49 |
// production error handler |
|
50 |
// no stacktraces leaked to user |
|
51 |
app.use(function(err, req, res, next) { |
|
52 |
res.status(err.status || 500); |
|
53 |
res.render('error', { |
|
54 |
message: err.message, |
|
55 |
error: {} |
|
56 |
}); |
|
57 |
}); |
|
58 | ||
59 | ||
60 |
module.exports = app; |