-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (59 loc) · 1.5 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
'use strict';
// Load modules
const Hapi = require('@hapi/hapi');
// Declare internals
const init = async () => {
// Create a server with a host and port
const server = Hapi.server({
port: 1234,
host: 'localhost'
});
// Add the route
server.route([{
method: 'GET',
path: '/',
handler: (request, h) => {
//return 'Hello World!';
return "<h1>Hello World!</h1>";
}
},{
method: 'GET',
path: '/users/{user?}',
handler: (request, h) => {
if (request.params.user) {
return `<h1>Hello ${request.params.user}</h1>`;
} else {
return `<h1>Hello Stranger!</h1>`;
}
}
},{
method: 'GET',
path: '/search/{search?}',
handler: (request, h) => {
return `<h1>You searched for ${request.query.name} ${request.query.lastname}</h1>`;
}
},{
method: 'GET',
path: '/home',
handler: (request, h) => {
return h.redirect('/');
}
},{
method: 'GET',
path: '/{any*}',
handler: (request, h) => {
return `<h1>404 Error! Page Not Found!</h1>`;
}
},
]);
// Start the server
await server.start();
console.log('Server running on %s', server.info.uri);
};
// Handle unhandled promise rejections
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
// Start the server
init();