forked from thanhhaimai/expressjs-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·80 lines (68 loc) · 2.03 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/** Setting up dependencies. */
var util = require('util');
var express = require('express');
var mongoose = require('mongoose');
var mongoStore = require('connect-mongodb');
/** .*/
var app = module.exports = express.createServer();
var config = app.config = require('./config');
var db;
/** Database models. */
require('./models').defineModels(mongoose, app, function() {
db = mongoose.connect(config.dburi);
});
/** .*/
process.addListener('uncaughtException', function(err, stack) {
util.log('Caught exception: ' + err + '\n' + err.stack);
console.log('\u0007');
});
/** Where to look for templates. */
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
/** Set up server, session management. */
app.use(express.favicon(__dirname + '/public/favicon.ico', {
maxAge: config.FAVICON_LIFETIME
}));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
secret: config.secret,
store: mongoStore(db)
}));
app.use(express.logger({
format: ':req[x-real-ip] :date (:response-time ms): :method :url'
}));
app.use(express.static(__dirname + '/public', {
maxAge: config.COOKIE_LIFETIME
}));
/** Load all the lib. */
require('./lib')(app);
app.use(app.router);
/** Show all errors and keep search engines out using robots.txt .*/
app.configure('development', function() {
app.use(express.errorHandler({
showStack: true,
dumpExceptions: true
}));
app.all('/robots.txt', function(req, res) {
res.send('User-agent: *\nDisallow: /', {
'Content-Type': 'text/plain'
});
});
});
/** Suppress errors, allow all search engines .*/
app.configure('production', function() {
app.use(express.errorHandler({
dumpExceptions: true
}));
app.all('/robots.txt', function(req, res) {
res.send('User-agent: *', {
'Content-Type': 'text/plain'
});
});
});
/** Load all the routes. */
require('./routes')(app);
/** Start listenning. */
app.listen(config.port);
util.log(util.format('ENV: %s, listening on http://%s:%s', config.env, app.address().address, app.address().port));