-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (65 loc) · 1.61 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
'use strict';
const Hapi = require('hapi');
const Good = require('good');
const mongoose = require('mongoose');
const ProductController = require('./controllers/microcenter/product');
const ScrapeController = require('./controllers/microcenter/scrape');
const MongoDBUrl = 'mongodb://localhost:27017/productapi';
const server = new Hapi.Server({
port: 3000
});
server.route({
method: 'GET',
path: '/',
handler: ProductController.list
});
server.route({
method: 'GET',
path: '/product/{id}',
handler: ProductController.get
});
server.route({
method: 'get',
path: '/count',
handler: ProductController.count
})
server.route({
method: 'POST',
path: '/product',
handler: ProductController.create
});
// create multiple products
server.route({
method: 'POST',
path: '/products',
handler: ProductController.createMany
});
// pass in the store id to scrape
server.route({
method: 'POST',
path: '/scrape',
handler: ScrapeController.scrape
});
// pass in the store id and category id to scrape
server.route({
method: 'POST',
path: '/category/count',
handler: ScrapeController.getPageCountForCategory
});
/*
server.register({
plugin: require('good-squeeze'),
plugin: require('good-console')
}).then(()=> {
});
*/
(async () => {
try {
console.log('starting server');
await server.start();
await mongoose.connect(MongoDBUrl, {}).then(() => { console.log(`Connected to Mongo server`) }, err => { console.log(err) });
console.log('finished server start');
} catch (e) {
console.log(e);
}
})();