-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
40 lines (32 loc) · 1.08 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
var express = require('express');
var app = express();
var Datastore = require('nedb');
var pages = require('./routes/pages.js');
var admin = require('./routes/admin.js');
var basicAuth = require('basic-auth');
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
var auth = function (req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.send(401);
};
var user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
};
if (user.name === 'admin' && user.pass === '1234') {
return next();
} else {
return unauthorized(res);
};
};
app.get('/', pages.getHomepage);
app.get('/admin/pages', auth, admin.showPages);
app.get('/admin/pages/edit/:slug', admin.editPage);
app.get('/admin/pages/add', auth, admin.addPage);
app.get('/:slug', pages.getPage);
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});