forked from naturalcrit/homebrewery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (66 loc) · 2.12 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
'use strict';
var _ = require('lodash');
require('app-module-path').addPath('./shared');
var vitreumRender = require('vitreum/render');
var bodyParser = require('body-parser')
var express = require("express");
var app = express();
app.use(express.static(__dirname + '/build'));
app.use(bodyParser.json());
//Mongoose
var mongoose = require('mongoose');
var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/naturalcrit';
mongoose.connect(mongoUri);
mongoose.connection.on('error', function(){
console.log(">>>ERROR: Run Mongodb.exe ya goof!");
});
//Admin route
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password';
process.env.ADMIN_KEY = process.env.ADMIN_KEY || 'admin_key';
var auth = require('basic-auth');
var HomebrewModel = require('./server/homebrew.model.js').model;
app.get('/admin', function(req, res){
var credentials = auth(req)
if (!credentials || credentials.name !== process.env.ADMIN_USER || credentials.pass !== process.env.ADMIN_PASS) {
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
return res.status(401).send('Access denied')
}
HomebrewModel.find({}, function(err, homebrews){
//Remove the text to reduce the response payload
homebrews = _.map(homebrews, (brew)=>{
brew.text = brew.text != '';
return brew;
});
vitreumRender({
page: './build/admin/bundle.dot',
prerenderWith : './client/admin/admin.jsx',
clearRequireCache : true,
initialProps: {
url: req.originalUrl,
admin_key : process.env.ADMIN_KEY,
homebrews : homebrews,
},
}, function (err, page) {
return res.send(page)
});
});
});
app = require('./server/homebrew.api.js')(app);
app.get('*', function (req, res) {
vitreumRender({
page: './build/naturalCrit/bundle.dot',
globals:{
},
prerenderWith : './client/naturalCrit/naturalCrit.jsx',
initialProps: {
url: req.originalUrl
},
clearRequireCache : true,
}, function (err, page) {
return res.send(page)
});
});
var port = process.env.PORT || 8000;
app.listen(port);
console.log('Listening on localhost:' + port);