-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathserver.js
171 lines (149 loc) · 3.92 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
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
'use strict';
var express = require('express');
var compression = require('compression');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var errorHandler = require('errorhandler');
var app = express();
var fs = require('fs');
var path = require('path');
var schedule = require('node-schedule');
schedule.scheduleJob('12 2 * * *', function() {
console.log('Running plugin updater...');
try {
require('./grunt-plugins').download();
} catch (e) {
console.log(e);
}
});
// enable express strict routing, see http://expressjs.com/api.html#app-settings
// for more info
app.enable('strict routing');
/**
* express app configuration
*/
app.use(compression());
app.use(methodOverride());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'src', 'tmpl'));
app.set('view engine', 'pug');
/**
* Strip trailing slashes
*
* Redirect "/foo/" to "/foo". Browsers interpret absolute paths in Location
* as relative to the current origin.
*
* Avoid redirecting to a paths that browsers may interpret as URLs to other sites,
* such as "//foo" or "http://". https://github.com/gruntjs/gruntjs.com/issues/231
*/
app.use(function (req, res, next) {
if (req.url.startsWith('/') && !req.url.startsWith('//') && req.url.length >= 2 && req.url.slice(-1) === '/') {
res.redirect(301, req.url.slice(0, -1));
} else {
next();
}
});
/**
* Server configuration
*/
var port = process.env.PORT || 5678;
app.listen(port);
console.log('Starting a server on port: ' + port);
/**
* express app router
*/
app.get('/', function (req, res, next) {
var filePath = 'build' + req.url + '.html';
if (req.url === '/') {
filePath = 'build/index.html';
}
fs.exists(filePath, function (exists) {
if (exists) {
res.sendFile(filePath, {root: __dirname});
} else {
next();
}
});
});
// api routes
app.get('/api*', function (req, res, next) {
req.url = req.url.toLowerCase();
var filePath = 'build' + req.url + '.html';
// redirect to the main api page, fix slashes and folder issues
if (req.url === '/api') {
res.redirect(301, '/api/grunt');
return;
}
fs.exists(filePath, function (exists) {
if (exists) {
res.sendFile(filePath, {root: __dirname});
} else {
next();
}
});
});
// news route
app.get('/blog*', function (req, res, next) {
var cleanUrl = req.url.split('?')[0];
var filePath = 'build' + cleanUrl + '.html';
if (cleanUrl === '/blog') {
res.sendFile('build/blog.html', {root: __dirname});
} else {
fs.exists(filePath, function (exists) {
if (exists) {
res.sendFile(filePath, {root: __dirname});
} else {
next();
}
});
}
});
// plugins route
app.get('/plugins*', function (req, res) {
res.sendFile('build/plugins.html', {root: __dirname});
});
// blm route
app.get('/blm*', function (req, res) {
res.sendFile('build/blm.html', {root: __dirname});
});
// rss atom feed
app.get('/rss', function (req, res) {
res.setHeader('Content-Type', 'application/xml');
res.setHeader('Charset', 'utf-8');
res.sendFile('build/atom.xml', {root: __dirname});
});
// final route, if nothing else matched, this will match docs
app.get('*', function (req, res, next) {
req.url = req.url.toLowerCase();
var filePath = 'build/docs/' + req.url + '.html';
if (req.url.indexOf('/grunt.') === 0) {
res.redirect(301, '/api' + req.url);
return;
}
fs.exists(filePath, function (exists) {
if (exists) {
res.sendFile(filePath, {root: __dirname});
} else {
next();
}
});
});
/**
* express app configuration after routes
*/
// use the static router
app.use(express.static('build'));
// if nothing matched, send 404
app.use(function (req, res) {
res.status(404).render('404', {
page: 'notfound',
title: '404 Not Found'
});
});
app.use(errorHandler({
dumpExceptions: false,
showStack: false
}));