-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (69 loc) · 2.13 KB
/
index.js
File metadata and controls
80 lines (69 loc) · 2.13 KB
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
const Hapi = require('hapi');
const HapiCron = require('hapi-cron');
const mongoose = require('mongoose');
var request = require('request-promise');
const Path = require('path');
const MongoDBUrl = 'mongodb://localhost:27017/imdb';
const server = Hapi.server({
port: parseInt(process.env.PORT, 10) || 3000,
host: 'localhost',
routes: {
cors: {
origin: ['*'],
additionalHeaders: ['sessionid', 'cache-control', 'x-requested-with']
},
validate: {
failAction: async (request, h, err) => {
if (process.env.NODE_ENV === 'production') {
var Boom = require('boom');
throw Boom.badRequest(`Invalid request payload input`);
} else {
throw err;
}
}
}
}
});
const init = async () => {
//auto loading routes directories
await server.register({
plugin: require('hapi-auto-route'),
options: {
routes_dir: Path.join(__dirname, 'routes')
}
});
await server.register(require('inert'));
// // cronjob to remove pin on job
// await server.register({
// plugin: HapiCron,
// options: {
// jobs: [
// {
// name: 'job',
// time: '0 */5 * * * *',
// timezone: 'Asia/Ho_Chi_Minh',
// request: {
// method: 'GET',
// url: '/crawl/reviews'
// },
// onComplete: (res) => {
// console.log(res);
// }
// },
// ],
// }
// });
await server.start();
console.log(`Server running at: ${server.info.uri}`);
//TODO add username and password
//mongoose.set('debug', true);
mongoose.connect(MongoDBUrl, {}).then(() => {
console.log('Connected to Mongo');
}, err => { console.log(err) });
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
module.exports = server;