-
Notifications
You must be signed in to change notification settings - Fork 27
/
server.js
45 lines (41 loc) · 1.31 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const multer = require('multer');
var server = require('http').createServer(app);
var port = process.env.PORT || 80;
app.use(bodyParser.urlencoded({
limit: "100mb",
extended: false
}));
app.use(bodyParser.json());
var ajaxUtilities = require('./app/js/ajax-utilities');
/**
* This function redirects requests from the application to the corresponding function
* located in ajax-utilities. The desired function is passed in the URL.
*/
function requestHandler(req, res){
// :fn holds the name of the function to call in ajax-utilities.js
var fn = req.params.fn || "";
if (typeof ajaxUtilities[fn] !== "function") {
// URL doesn't point to an actual function
res.sendStatus(400);
}
else{
try {
ajaxUtilities[fn](req, res);
}
catch (err) {
// this cannot catch the errors thrown INSIDE the function called above
// if this function performs ASYNCHRONOUS things
console.log("Error with call to /utilities/"+fn+" with query: "+req.query, err);
res.sendStatus(500);
}
}
}
app.get('/utilities/:fn', requestHandler);
app.post('/utilities/:fn', requestHandler);
server.listen(port, function(){
console.log('server listening on port: %d', port);
});
app.use(express.static(__dirname, {dotfiles: 'ignore'}));