-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (105 loc) · 3.19 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const Path = require('path');
const Hapi = require('hapi');
const Joi = require('joi');
const Inert = require('inert');
const Vision = require('vision');
const Handlebars = require('handlebars');
const _ = require('underscore');
const server = Hapi.server({
port: 3000,
routes: {
files: {
relativeTo: Path.join(__dirname, 'public')
}
}
});
const start = async() => {
await server.register(Inert);
await server.register(Vision);
//error handling
server.ext('onPreResponse', function (request, h) {
const response = request.response;
// if there's no Boom error, don't bother checking further down
if (!response.isBoom) {
return h.continue;
}
//let's handle login POST error
if (request.route.path == '/login' && request.route.method == 'post') {
//this is not very elegant, but I struggled to make the error checking less convoluted.
const isNameEmpty = _.where(response.details, {message: '"username" is not allowed to be empty'}).length > 0;
const isNameNotEmail = _.where(response.details, {message: '"username" must be a valid email'}).length > 0;
const isPasswordEmpty = _.where(response.details, {message: '"password" is not allowed to be empty'}).length > 0;
return h.view('login', {
error: {
isNameError: isNameEmpty || isNameNotEmail,
isNameEmpty: isNameEmpty,
isNameNotEmail: isNameNotEmail,
isPasswordEmpty: isPasswordEmpty
}
});
}
return h.continue;
});
server.views({
engines: {
hbs: Handlebars
},
relativeTo: Path.join(__dirname, 'templates'),
isCached: false,
partialsPath: 'partials',
helpersPath: 'helpers',
layout: true
});
server.route({
method: 'get',
path: '/',
handler: (request, h) => {
return h.view('index', {
title: "Index"
})
}
});
server.route({
method: 'GET',
path: '/login',
handler: (request, h) => {
return h.view('login', {title: "login"})
}
});
server.route({
method: 'POST',
path: '/login',
config: {
validate: {
options: {
abortEarly: false
},
payload: {
username: Joi.string().email().required(),
password: Joi.string().required()
},
failAction: (request, h, err) => {
throw err;
return;
}
}
},
handler: (request, h) => {
return h.view('login', {title: "login"})
}
});
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true
}
}
});
await server.start();
console.log("Hapi is running on port", server.info.uri);
};
start();