This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
executable file
·69 lines (52 loc) · 2.19 KB
/
web.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
var express = require("express"),
querystring = require("querystring"),
v1 = require(__dirname + "/v1/router.js");
require(__dirname + "/api/database.js"), // to trigger the setup
require(__dirname + "/api/ping.js"); // to keep the app awake
var app = express.createServer();
// Basic request preparation and stuffing post data into req
var prepareRequests = function(request, response, next) {
request.ip = request.headers["x-forwarded-for"] || request.connection.remoteAddress;
// cross-origin request headers
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
response.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
// post data
if(request.method != "POST" || (request.body && request.body.data)) {
next();
return;
}
var chunk = "";
request.on("data", function (data) {
chunk += data;
});
request.on("end", function () {
request.body = querystring.parse(chunk);
next();
});
};
// Configuration
app.configure(function(){
app.use(prepareRequests);
app.use(express.bodyParser());
app.use(app.router);
});
app.configure("production", function(){
process.on("uncaughtException", function (exceptionmessage) {
console.log("EXCEPTION: \n" + exceptionmessage);
});
});
// start
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port + ", env: " + process.env.NODE_ENV + ", local: " + (process.env.local || false));
});
// cross domain
var crossdomain = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><site-control permitted-cross-domain-policies=\"master-only\" /><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><allow-http-request-headers-from domain=\"*\" headers=\"*\" /></cross-domain-policy>";
var XML_HEADER = {"Content-Type": "text/xml"};
app.all("/crossdomain.xml", function(request, response) {
response.writeHead(200, XML_HEADER);
response.end(crossdomain);
});
// everything else
app.all("/v1", v1.router);