-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
176 lines (149 loc) · 4.09 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
var express = require('express');
var jade = require('jade');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var errorhandler = require('errorhandler');
jade.filters.highlight = require('./lib/jade-highlight');
var app = express();
// default config
app.set('port', process.env.PORT || 3000);
app.set('webfonts', true);
// override those config if they're passed as arguments
var args = process.argv.splice(2);
parseArguments: while (args.length){
var arg = args.shift();
switch (arg){
case '-p':
case '--port':
app.set('port', args.shift());
break;
case '--webfonts':
case '--webfonts=false':
case '--webfonts=true':
app.set('webfonts', arg != '--webfonts=false');
break;
default:
break parseArguments;
}
}
// all environments
app.set('views', __dirname + '/views');
app.engine('jade', jade.__express);
app.set('view engine', 'jade');
app.set('view cache', true);
app.use(favicon(__dirname + '/public/images/favicon/mootools.ico'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// fix trailing slashes in path
app.use(function(req, res, next){
if (req.path != '/' && req.path.substr(-1) == '/'){
var query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});
// important to be before express.router
if (app.get('env') == 'development'){
app.use(morgan('dev'));
app.use(errorhandler());
app.use(function setJadePretty(req, res, next){
res.locals.pretty = true;
res.locals.cache = false;
next();
});
}
app.locals.dateable = require('dateable');
app.locals.webfonts = app.get('webfonts');
// stuff to build static files (css/js)
require('./middleware/build-static')(app, {
dirname: __dirname
});
app.use(express.static(__dirname + '/public', {redirect : false}));
// github, twitter & blog feeds
var githubEvents = require('./middleware/githubEvents')({
org: 'mootools'
});
var twitter = require('./middleware/twitter')();
var blogData = require('./blog/data');
function getLatestBlog(req, res, next){
blogData.get(function(err, blog) {
if (err) next(err);
res.locals.lastBlogPost = blog.posts[0];
next();
});
}
// home
app.get('/', githubEvents, twitter, getLatestBlog, function(req, res){
res.render('index', {
title: 'MooTools',
site: 'mootools',
lastBlogPost: res.locals.lastBlogPost,
tweetFeed: res.locals.twitter
});
});
app.get('/search', function(req, res){
res.render('search', {
title: 'Search',
page: 'search'
});
});
require('./core')(app);
require('./more')(app);
require('./blog')(app);
require('./books')(app);
require('./builder')(app);
require('./developers')(app);
// redirect old docs path
var projects = require('./package.json')._projects;
app.get('/docs/:project?/:module?/:file?', function(req, res){
var project = projects[req.params.project] ? req.params.project : 'core';
var latestVersion = projects[project].versions[0];
if (!req.params.project) res.redirect(301, '/core/docs' + latestVersion);
var newPath;
if (!req.params.file){
newPath = ['/core/docs', latestVersion, req.params.project, req.params.module].join('/');
} else {
newPath = ['', req.params.project, 'docs', latestVersion, req.params.module, req.params.file].join('/');
}
res.redirect(301, newPath);
});
// redirect old forge path
app.get(/^\/plugins/, function(req, res){
res.redirect(301, req.path.replace('/plugins', '/forge'));
});
// redirect old download paths
app.get(/^\/download/i, function(req, res){
res.redirect(301, '/core');
});
// handle 404 errors
app.get('*', function(req, res, next){
var err = new Error();
err.status = 404;
next(err);
});
app.use(function(err, req, res, next){
if (err.status != 404){
return next(err);
}
res.status(404);
res.render('errors/404', {
site: 'mootools'
});
});
if (app.get('env') != 'development'){
app.use(function(err, req, res, next){
res.status(500);
res.render('errors/500', {
site: 'mootools'
});
});
}
// starting server
app.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});