-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·126 lines (113 loc) · 3.48 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
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
var sys = require('sys');
var http = require('http');
var querystring = require('querystring');
var express = require('express');
var request = require('request');
var database = require('./database');
var xml2js = require('xml2js');
var config = database.loadJSON(__dirname + "/siteconf.json", "utf-8");
var dataDir = config.dataDir || __dirname + "/fixtures";
var db = database.init(dataDir);
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/', function(req, res){
res.render('exhibit', {
page: {id: 'index', title: 'MuseumVille'},
exhibit: db.newEmptyExhibit(),
curator: null
});
});
app.get('/search', function(req, res) {
var searchString = req.param('q');
if (!searchString) {
res.render('search', {
items: [],
searchString: searchString,
page: {
id: 'search',
title: 'Add to your exhibit'
}
});
return;
}
var searchUri = 'http://api.europeana.eu/api/opensearch.rss?' +
querystring.stringify({
searchTerms: searchString,
qf: 'TYPE:IMAGE',
wskey: config.europeana.apiKey});
request({uri: searchUri}, function(error, clientRes, body) {
if (!error && clientRes.statusCode == 200) {
var parser = new xml2js.Parser();
parser.addListener('end', function(result) {
res.render('search', {
items: result.channel.item,
searchString: searchString,
page: {
id: 'search',
title: 'Add to your exhibit'
}
});
});
parser.parseString(body);
} else {
console.log("Could not contact Europeana");
res.send(500); return;
}
});
});
app.get('/exhibits', function(req, res) {
var bill = db.users['bill'];
var ted = db.users['ted'];
var users = [bill, ted];
res.render('exhibits', {
page: {id: 'exhibits', title: "MuseumVille - Exhibits" },
users: users
});
});
app.get('/:userId', function(req, res) {
var data = db.users[req.params.userId];
if (!data) { res.send(404); return; }
res.render('curator', {
page: {id: 'curator', title: "MuseumVille - " + data.name},
curator: data
});
});
app.all('/:userId/exhibit/:exhibitId', function(req, res, next) {
req.curator = db.users[req.params.userId];
if (!req.curator) { res.send(404); return; }
req.exhibit = req.curator.exhibits[parseInt(req.params.exhibitId)];
if (!req.exhibit) { res.send(404); return; }
next();
});
app.get('/:userId/exhibit/:exhibitId', function(req, res) {
for (var i=req.exhibit.items.length; i < 5; i++) {
req.exhibit.items.push(db.newEmptyItem());
}
res.render('exhibit', {
page: {id: 'exhibit',
title: "MuseumVille - " + req.curator.name + " - " + req.exhibit.name},
curator: req.curator,
exhibit: req.exhibit
});
});
app.post('/:userId/exhibit/:exhibitId', function(req, res) {
if (req.accepts('json') && req.body.subject) {
db.addItem(req.curator, req.exhibit, req.body);
res.send(200);
}
});
app.listen(config.serverPort);
console.log("Express server listening on port %d", app.address().port);