-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·46 lines (41 loc) · 1.38 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
#!/usr/bin/env node
var connect = require('connect');
var fs = require('fs');
var handlebars = require('handlebars');
var defaultFile = "/gallery/index.html";
var httpPort = 8085;
var httpHost = "localhost";
var staticDir = "app";
var routes = {
home: /^\/$/,
photo: /^\/photos\/.+/,
static: /\.(?:js|jpg|png|json|css|ico|html|manifest|mp3|txt)(?:\?.+)?$/,
wildcard: /.*/
};
// TODO tell nodemon to restart when this changes
var homeTemplate = fs.readFileSync('app/gallery/index.html', 'utf8');
var template = handlebars.compile( homeTemplate );
function route(app) {
app.get(routes.photo, function(request, response, next) {
// TODO lookup description from same datasource as client
// also use that to generate list of images on index
response.end(template({indexActive: "", src: request.url.replace("/photos", "") + ".jpg", description: "..."}));
});
app.get(routes.home, function(request, response, next) {
response.end(template({indexActive: "indexActive"}));
});
// TODO drop wildcard
app.get(routes.wildcard, function(request, response, next) {
if (!routes.static.test(request.url)) {
request.url = defaultFile;
}
next();
});
}
connect.createServer(
connect.router(route),
//connect.logger(),
connect.static(staticDir)
).listen(httpPort, httpHost, function() {
console.log('HTTP Server running at http://%s:%d', httpHost, httpPort);
});