-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
166 lines (149 loc) · 5.22 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
require('babel-core/register');
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
fs = require('fs'),
async = require('async'),
match = require('react-router').match,
React = require('react'),
ReactDOMServer = require('react-dom/server'),
RoutingContext = React.createFactory(require('react-router').RoutingContext),
routes = require('./lorrainesposto/routes');
var mongoose = require('mongoose');
// My things
var Recipe = require('./models/recipe');
var Ingredient = require('./models/ingredient');
var server = express();
var router = express.Router();
var root_dir = path.join(__dirname, 'lorrainesposto');
/*
mongoose.connect('mongodb://localhost/recipes', function (err, db) {
if (!err) {
console.log('Connected to mongo');
}
});
// load fixture
mongoose.connection.once('connected', function () {
Recipe.count({}, function (err, c) {
// Count recipes and drop
if (err) console.log('Error retrieving Recipe count.');
if (c > 0) {
console.log('Removing old data from Recipes.');
Recipe.remove({}, function (err) {
if (err) console.log('Error removing all from Recipes.');
});
}
});
Ingredient.count({}, function (err, c) {
if (err) console.log('Error retrieving Ingredient count.');
if (c > 0) {
console.log('Removing old data from Ingredient.');
Ingredient.remove({}, function (err) {
if (err) console.log('Error removing all from Ingredient.');
});
}
});
// read fixture and populate
fs.readFile('./fixture/fixture.json', function (err, data) {
if (err) throw err;
var fix = JSON.parse(data);
async.forEachOf(fix, function(item, id, cb1) {
async.forEachOf(item, function(obj, key, cb2) {
if (key === 'Recipe') {
new Recipe(obj).save(function (err) {
if (err) {
cb2(err);
}
cb2();
});
}
else if (key === 'Ingredient') {
new Ingredient(obj).save(function (err) {
if (err) {
cb2(err);
}
cb2();
});
}
}, function(err) {
if (err) {
cb1(err);
}
cb1();
});
}, function(err) {
if (err) {
console.log(err);
}
console.log("Finished loading data fixture.");
});
});
});*/
// view engine setup
server.set('views', path.join(root_dir, 'views'));
server.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//server.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
server.use(logger('dev'));
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({extended: false}));
server.use(cookieParser());
server.use('/public', express.static(path.join(root_dir, '/public')));
server.use('/images', express.static(path.join(__dirname, 'fixture', 'images')));
//server.use('/public', express.static(path.join(root_dir, 'bower_components')));
server.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
match({routes, location: req.url}, (err, redirectLocation, renderProps) => {
if (err) {
res.status(500).send(err.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
console.log("Rendering", req.url);
res.status(200).render('index', {
reactContent: ReactDOMServer.renderToString(RoutingContext({
history: renderProps['history'],
location: renderProps['location'],
components: renderProps['components'],
routes: renderProps['routes'],
params: renderProps['params']
}))
// dis shit
});
} else {
//res.status(404).send('Not found')
console.log("404 Not Found:", req.url);
var err = new Error('Not Found');
err.status = 404;
next(err);
}
});
});
// error handlers
// development error handler
// will print stacktrace
if (server.get('env') === 'development') {
server.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
title: 'Error',
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
server.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
title: 'Error',
message: err.message,
error: {}
});
});
module.exports = server;