-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·70 lines (63 loc) · 2.06 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
var connect = require('connect');
var fs = require('fs');
var handlebars = require('handlebars');
var url = require('url');
var formidable = require('formidable');
var util = require('util');
var photos = require('./app/gallery/photos');
var pair = require('./lib/sound-image-pair');
var httpPort = 8085;
var httpHost = "localhost";
var staticDir = "app";
var routes = {
home: "/",
photo: /^\/photos\/.+/,
pair: "/pair",
pairResult: "/pair/result",
errorlogger: "/errorlogger"
};
// TODO tell nodemon to restart when these change
var homeTemplate = handlebars.compile( fs.readFileSync('app/gallery/index.html', 'utf8') );
var pairTemplate = handlebars.compile( fs.readFileSync('app/sound-image-pair/index.html', 'utf8') );
function route(app) {
app.get(routes.photo, function(request, response, next) {
var photo = photos.lookup(request.url);
response.end(homeTemplate({
src: photo.src,
description: photo.description,
photos: photos.data
}));
});
app.get(routes.home, function(request, response, next) {
response.end(homeTemplate({
indexActive: "indexActive",
photos: photos.data
}));
});
app.get(routes.pair, function(request, response, next) {
var term = url.parse(request.url, true).query.term || 'bird';
pair(term, function(result) {
result.term = term;
response.end(pairTemplate(result));
});
});
app.get(routes.pairResult, function(request, response, next) {
pair(url.parse(request.url, true).query.term, function(result) {
response.end(JSON.stringify(result));
});
});
app.post(routes.errorlogger, function(request, response, next) {
var form = new formidable.IncomingForm();
form.parse(request, function(err, fields, files) {
console.log("[CLIENT " + fields.type + " error]", fields.message, fields.detail);
response.end("");
});
});
}
connect.createServer(
connect.router(route),
connect.static(staticDir)
).listen(httpPort, httpHost, function() {
console.log('HTTP Server running at http://%s:%d', httpHost, httpPort);
});