-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (141 loc) · 3.38 KB
/
index.js
File metadata and controls
154 lines (141 loc) · 3.38 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const hapi = require('hapi');
const vision = require('vision');
const inert = require('inert');
const handlebars = require('handlebars');
const Path = require('path');
const pg = require('pg');
const Bcrypt = require('bcrypt');
const Basic = require('hapi-auth-basic');
const Joi = require('joi');
const server = new hapi.Server();
const Joi = require ('joi')
const config = {
user: 'postgres',
password: '123456',
host: 'localhost',
port: '5432',
database: 'facebook'
};
const client = new pg.Client(config);
const selectdata = (client, query, cb) => {
client.query(query, cb);
};
const query = `SELECT * FROM info ;`;
server.connection({
host: 'localhost',
port: '3000'
});
client.connect(err => {
if (err) {
throw err;
}
});
const users = {
john: {
username: 'john',
password: '$2a$10$R7U2Lwe7PUa3h101TvzZuujS6e.nXnT5xhfWH1W9Ct86IvXJz84KS', // 'secret'
name: 'John Doe',
id: '2133d32a'
},
ahmed: {
username: 'admin'
}
};
const validate = function(request, username, password, callback) {
const user = users[username];
if (!user) {
return callback(null, false);
}
Bcrypt.compare(password, user.password, (err, isValid) => {
callback(err, isValid, {
id: user.id,
name: user.name
});
});
};
server.register(Basic, (err) => {
if (err) {
throw err;
}
server.auth.strategy('simple', 'basic', {
validateFunc: validate
});
server.route({
method: 'GET',
path: '/',
config: {
auth: 'simple',
cache: {
expiresIn: 30 * 1000,
privacy: 'private'
},
handler: function (request, reply) {
selectdata(client, query, function(err, result) {
reply(result.rows).state('data','akram', { encoding: 'none' });
})
}
}
});
server.route({
method: 'GET',
path: '/users/{name}',
handler: (request, reply) => {
console.log('cookie: ',"test");
const name = encodeURIComponent(request.params.name);
reply.view("users", {
name: name
});
}
});
});
server.route({
method: 'POST',
path: '/hello',
config: {
handler: (request, reply) => {
reply.view("ghada");
},
validate: {
payload: {
fullname: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
email: Joi.string().email()
}
}
}
});
server.route({
method: 'GET',
path: '/hello',
handler: (request, reply) => {
const name = encodeURIComponent(request.params.name);
reply.view("hello");
}
});
server.register(vision, (err) => {
if (err) {
throw err
}
});
server.register(inert, (err) => {
server.route({
method: 'GET',
path: '/google.png',
handler: (request, reply) => {
reply.file('google.png');
}
});
})
server.views({
engines: {
html: require('handlebars')
},
relativeTo: __dirname,
path: 'templates'
});
server.start((err) => {
if (err) {
throw err
}
console.log('Server is running at :' + server.info.uri);
});