-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
171 lines (135 loc) · 3.99 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
167
168
169
170
171
/**
* Module dependencies
*/
var express = require('express'),
morgan = require('morgan'),
routes = require('./routes'),
api = require('./routes/api'),
http = require('http'),
path = require('path'),
parser = require('xml2json'),
request = require('request'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
var app = module.exports = express();
/**
* Schemas
*/
var Schema = mongoose.Schema;
var articleSchema = new Schema({
articleId: Number,
name: String,
price: Number,
volume: Number,
alcoholPercent: String,
prodType: String,
producer: String
});
var Article = mongoose.model('Article', articleSchema);
var commentSchema = new Schema({
commentId: Number,
body: String,
date: Date,
userId: Number
});
var Comment = mongoose.model('Comment', commentSchema);
/**
* Hooks
*/
articleSchema.pre('save', function(next){
var self = this;
Article.find({articleId: self.articleId}, function(err, docs){
if(!docs.length){
next();
} else {
console.log('article exists: ', self.articleId + ' ' + self.name);
next(new Error("Article exists!"));
}
});
});
/**
* Configuration
*/
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(morgan('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({ strict: true }));
/**
* Routes
*/
// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:articleId', routes.article);
app.get('/test', routes.test);
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
// Update database API
app.post('/api/updatedb', function(){
var url = 'http://www.systembolaget.se/api/assortment/products/xml';
request(url, function (error, response, body) {
console.log('Fetching XML from ' + url);
if (!error && response.statusCode == 200) {
var result = parser.toJson(body, {
object: true,
reversible: false,
coerce: true,
sanitize: false,
trim: true,
arrayNotation: false
});
var articleList = result.artiklar;
for(i = 0; i < articleList.artikel.length; i++) {
var article = new Article({
articleId: articleList.artikel[i].Artikelid,
name: articleList.artikel[i].Namn,
price: articleList.artikel[i].Prisinklmoms,
volume: articleList.artikel[i].Volymiml,
alcoholPercent: articleList.artikel[i].Alkoholhalt,
prodType: articleList.artikel[i].Varugrupp,
producer: articleList.artikel[i].Producent
});
article.save(function(err){
if(err){
console.log('Error on save. Reason: ' + err);
}
});
};
console.log("Database updated");
};
});
});
app.post('/api/getarticle', function(req, res){
var query = req.body;
Article.find(query, function(err, article){
if(err){
return console.error(err);
} else {
res.send(article);
}
});
});
app.post('/api/getarticles', function(req, res){
Article.find(function(err, articles){
if(err){
return console.error(err);
} else {
res.send(articles);
}
});
});
/**
* Connect to database
* Start Server
*/
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection error:'));
db.once('open', function(){
console.log('Connection established');
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});