-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
126 lines (109 loc) · 3.5 KB
/
api.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
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
async = require('async'),
fs = require('fs');
var routes = require('./api/routes');
var app = express();
var mongoose = require('mongoose');
// My things
var Recipe = require('./models/recipe');
var Ingredient = require('./models/ingredient');
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.");
});
});
});
// uncomment after placing your favicon in /public
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With,content-type");
next();
});
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.log(err);
console.log(req.url);
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
console.log(err);
});
module.exports = app;