-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
68 lines (61 loc) · 1.95 KB
/
main.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
/**
*
*/
console.log('Starting '+JSON.stringify(process.versions));
const url = require('url')
,path = require('path')
,favicon = require('serve-favicon')
,alasql = require('./lib/alasqlextended');
var express = require('express')
, http = require('http')
, app = express();
app.set('port', (process.env.PORT || process.env.VCAP_APP_PORT || 3000));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
function errorHandler(err, req, res, next) {
console.error('errorHandler '+err+(err.stack?err.stack:""));
if (res.headersSent)
return next(err);
res.status(500);
res.render('error', { error: "Internal error" });
}
app.use(errorHandler);
// development only
if ('development' === app.get('env')) {
console.log('development mode');
global.bypassSignon=true;
// app.use(express.errorHandler());
}
app.all('*', function (req, res, next) {
if (global.bypassSignon || req.session && req.session.authorized)
return next();
res.status(401);
res.render('error',{error:"Signon Required"});
});
function callAlasql(request, response, commands, next) {
console.log("callAlasql: " +JSON.stringify(commands));
var a;
try {
a = alasql(commands);
} catch(err) {
a = err.toString();
};
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify(a));
next();
}
alasql.extensions.initialise();
app.get('/commands/:commands'
,function(request, response, next) {
console.log("url:" + request.url);
// var command=require('url').parse(request.url,true).query; req.params.
// callAlasql(request, response, decodeURI(require('url').parse(request.url,true).select), next);
callAlasql(request, response, request.params.commands, next);
}
).post('/commands'
,function(request, response, next) {
callAlasql(request, response, request.body, next);
}
);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});