-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (50 loc) · 1.32 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
var Hapi = require('hapi');
var fs = require('fs');
var path =require('path');
var Image = require('./img');
var handlebars = require('handlebars');
var port = process.env.PORT || 8000;
var server = new Hapi.Server(port,{
views: {
engines: { html: handlebars },
path: './views'
},
state: {
cookies: {
failAction: 'ignore'
}
}
});
//加载静态资源
server.route([
{ method: 'GET', path: '/favicon.ico', handler: { file: __dirname +'/public/favicon.ico'} },
{ method: 'GET', path: '/public/{file*}', handler: function(request,reply){
var filePath = './public/' + request.params.file;
fs.exists(filePath, function (exist){
if (exist) {
reply.file(filePath);
} else {
reply("404 not found!").code(404);
}
});
}
}
]);
server.route({
method: 'GET',
path: '/img',
handler: function (request, reply){
var canvas = new Image(request.query).gen();
reply(canvas.toBuffer()).type('image/png');
}
});
server.route({
method : 'GET',
path : '/{p*}',
handler: function (request, reply){
reply.view('index',{page:[1,2]});
}
});
server.start(function(){
console.log("work @" +server.info.uri);
});