-
Notifications
You must be signed in to change notification settings - Fork 30
/
app.js
89 lines (74 loc) · 2.39 KB
/
app.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
81
82
83
84
85
86
87
88
89
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, cluster = require('cluster');
app = express();
app.configure(function(){
app.set('port', 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(require('./middleware/templates')(path.join(__dirname, 'views')));
app.use(express.static(path.join(__dirname, 'public')));
require('./routes');
app.use(app.router);
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
console.error('404 - ', req.url, req.headers.referer);
res.render('404.ejs', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
// error-handling middleware, take the same form
// as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).
// when connect has an error, it will invoke ONLY error-handling
// middleware.
// If we were to next() here any remaining non-error-handling
// middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware
// would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next) {
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.status(err.status || 500);
res.render('500.ejs', { data: {error: err} });
});
});
app.configure('development', function(){
app.use(express.errorHandler());
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var Index = require('./lib/indexer');
var index = app.index = new Index();
index.crawl('docs', function (cache) {
app.docs = cache;
});
/**
* nodefly
*/
if(process.env.NODEFLY_ID) {
require('nodefly').profile(
process.env.NODEFLY_ID,
['docs', 'docs.deployd.com', cluster.worker.id]
);
}