forked from hectorcorrea/hectorcorrea.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (65 loc) · 2.41 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
var path = require('path');
var http = require('http');
var logger = require('log-hanging-fruit').defaultLogger;
var legacyRoutes = require('./routes/legacyRoutes');
var siteRoutes = require('./routes/siteRoutes');
var blogRoutes = require('./routes/blogRoutes');
var userRoutes = require('./routes/userRoutes');
var testCounter = 0;
// Set the path for the log files
var options = {filePath: path.join(__dirname, 'logs') };
logger.setup(options);
// Configure Express settings
var app = require('./configure').app;
// Authentication middleware
var authenticate = function(req, res, next) {
req.isAuth = false;
if(req.cookies && req.cookies.authToken) {
// Make sure the session is valid.
userRoutes.validateSession(req, res, next);
}
else {
// Not authenticated, nothing else to do.
next();
}
}
// Legacy Routes (redirect to new URLs)
app.get('/about', legacyRoutes.about);
app.get('/blog', legacyRoutes.blogAll)
app.get('/blog/rss', blogRoutes.rss);
app.get('/blog/:url', legacyRoutes.blogOne);
// Blog routes (for Angular.js client)
app.get('/api/blog/all', authenticate, blogRoutes.all);
app.get('/api/blog/:url/:key', authenticate, blogRoutes.one);
app.get('/api/blog/:url/:key/edit', authenticate, blogRoutes.one);
app.post('/api/blog/:url/:key/draft', authenticate, blogRoutes.draft);
app.post('/api/blog/:url/:key/post', authenticate, blogRoutes.post);
app.post('/api/blog/:url/:key', authenticate, blogRoutes.save);
app.post('/api/blog/new', authenticate, blogRoutes.newOne);
// Login and authentication (for Angular.js client)
app.post('/api/user/changePassword', authenticate, userRoutes.changePassword);
app.post('/api/login', userRoutes.login);
app.post('/api/logout', authenticate, userRoutes.logout);
// Our humble home page (HTML)
app.get('/', function(req, res) {
logger.info('home page for ' + req.url);
if(process.env.NODE_ENV == 'production') {
res.app.settings.setCache(res, 5);
}
res.render('index')
});
// All others get a horrible 404
app.get('*', function(req, res) {
logger.error('Not found: ' + req.url);
if(process.env.NODE_ENV == 'production') {
res.app.settings.setCache(res, 5);
}
res.status(404).render('index.ejs', { error: 'Page not found' });
});
// Fire up the web server!
var server = http.createServer(app);
var port = app.get('port');
server.listen(port, function() {
var address = 'http://localhost:' + port;
logger.info('Express listening at: ' + address);
});