-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (61 loc) · 2.12 KB
/
server.js
File metadata and controls
74 lines (61 loc) · 2.12 KB
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
var express = require('express');
var to_json = require('xmljson').to_json;
var path = require('path');
var Keen = require('keen-js');
var favicon = require('serve-favicon');
var logger = require('morgan');
var methodOverride = require('method-override');
var session = require('express-session');
var bodyParser = require('body-parser');
var multer = require('multer');
var errorHandler = require('errorhandler');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(methodOverride());
app.use(session({ resave: true,
saveUninitialized: true,
secret: 'uwotm8' }));
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());
app.use(express.static(path.join(__dirname, 'public')));
// error handling middleware should be loaded after the loading the routes
if ('development' == app.get('env')) {
app.use(errorHandler());
}
app.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
//routes
app.get("/", function(req, res) {
res.render("home.jade");
});
//**************configure keen.io = replace with your data
var client = new Keen({
projectId: "<projectId>",
writeKey: "<API writeKey>"
});
app.post("/", function(req, res, next) {
var xml = req.body;
to_json(xml, function (error, data) {
// Module returns a JS object
console.log(data);
// confirm it's working in local host, displays as -> { prop1: 'val1', prop2: 'val2', prop3: 'val3' }
// Format as a JSON string and sends it at as response, so you can test in a REST client, you might want to change the response here
res.send(JSON.stringify(data));
// -> {"prop1":"val1","prop2":"val2","prop3":"val3"}
//send to keen
client.addEvent("<collection name>", data, function(err, res) {
if (err) {
console.log("Oh no, an error!");
} else {
console.log("Hooray, it worked!");
}
});
});
});