-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (126 loc) · 3.67 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
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
var Hapi = require('hapi');
var fs = require('fs');
var Request = require('request');
var dream = require('dreamjs');
var server = new Hapi.Server();
server.connection({ port: process.env.PORT || 5000 });
var dogwaterOptions = {
connections: {
userDB: {
adapter: 'userDisk'
}
},
adapters: {
userDisk: 'sails-disk'
},
defaults: {
migrate: 'safe'
},
models: [require('./models/user')]
};
server.register([
{
register: require('inert')
},
{
register: require('blipp')
},
{
register: require('dogwater'),
options: dogwaterOptions
},
{
register: require('bedwetter'),
options: {}
}
], function (err) {
if (err) {
console.log(err);
throw err;
}
server.route(require('./routes/user'));
server.route({
method: 'POST',
path: '/uploadfile',
config: {
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data'
},
handler: function (request, reply) {
var data = request.payload;
if (data.file) {
var name = data.file.hapi.filename;
var path = __dirname + "/public/avatars/" + name;
var file = fs.createWriteStream(path);
file.on('error', function (err) {
console.error(err)
});
data.file.pipe(file);
data.file.on('end', function (err) {
var ret = {
filename: data.file.hapi.filename,
headers: data.file.hapi.headers
}
reply(JSON.stringify(ret));
})
}
}
}
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('./public/index.html');
}
});
server.route({
method: 'GET',
path: '/fillusers',
handler: function (request, reply) {
var schema = {
first_name: 'name',
last_name: 'last',
email: 'email',
birthday: 'birthday',
password: 'string',
avatar: function () {
return encodeURIComponent('http://lorempixel.com/200/200/?') + Math.random();
}
};
dream.schema(schema)
.generateRnd()
.output(function (err, result) {
request.model.user.create(result).exec(function(err, r){
reply(r[0]);
});
});
}
});
server.route({
method: 'GET',
path: '/js/{filename}',
handler: function (request, reply) {
reply.file('./public/angularControllers/' + request.params.filename);
}
});
server.route({
method: 'GET',
path: '/avatars/{filename}',
handler: function (request, reply) {
if (request.params.filename.indexOf('http')>=0) {
Request(decodeURIComponent(request.params.filename))
.on('response', function (response) {
reply(response);
});
} else {
reply.file('./public/avatars/' + request.params.filename);
}
}
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
});