-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
50 lines (42 loc) · 1.17 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
const Hapi = require('hapi');
const productRoutes = require('./routes/product.routes');
const mongoose = require('mongoose');
// Connection to MongoDB Database
mongoose.connect('mongodb://localhost:27017/hapi_db');
mongoose
.connection
.on('connected', () => {
console.log('Connected to MongDB!');
});
mongoose
.connection
.on('error', (err) => {
console.log('Error while connecting to mongDB', err);
});
// Init Server
const server = new Hapi.Server({port: 8000, host: 'localhost'});
// Add a Route
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Welcome to HAPI API';
}
});
// Dynamic Route
server.route(productRoutes)
// Start server
const start = async() => {
try {
// await server.register(require('inert')); // Serve Static Files
await server.start();
} catch (error) {
console.log(error);
process.exit(1);
}
// Serve Static Files server.route({ method: 'GET', path: '/about',
// handler: (request, h) => { return h.file('./public/about.html');
// } });
console.log('Server running at:', server.info.uri);
}
start();