-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (43 loc) · 1.24 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require('express');
var mongoose = require('mongoose');
const app = express();
// Create connection for mongoose and specific Mongo database
mongoose.connect('mongodb://localhost:27017/macy_scrape', {
useMongoClient: true,
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// Define schema of database
var Schema = mongoose.Schema;
var itemSchema = new Schema({
name: String,
original_price: {
min: {type: Number, min: 0},
max: {type: Number, min: 0}
},
sale_price: {
min: {type: Number, min: 0},
max: {type: Number, min: 0}
}
});
// Create mongoose model based on schema
var Item = mongoose.model('Item', itemSchema, 'items');
// Define routes
app.get('/', function (req, res) {
res.send('Go to localhost:3000/home to see HTTP response output.');
})
app.get('/home', function(req, res) {
// Query all documents from collection and send JSON response
Item.find(function (err, items){
if (err) {
res.status(500).send(err);
} else {
console.log(items.length + ' items found.');
item_json = {"items": items};
res.json(item_json);
}
});
})
app.listen(3000, function () {
console.log('App listening on port 3000');
})