Created
September 24, 2016 21:37
-
-
Save mkotsollaris/6e7599879e4fbb1a36443a6a0cc0b217 to your computer and use it in GitHub Desktop.
index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Module dependencies. | |
*/ | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var app = require('../app'); | |
var debug = require('debug')('MedHacksBackend:server'); | |
var http = require('http'); | |
var Schema = mongoose.Schema; | |
app.use(bodyParser.json()); // to support JSON-encoded bodies | |
var medhacksSchema = new Schema({ | |
duration: String, | |
stream: [{1: String, 2: String}] | |
}); | |
var json_GET = "{\"duration\": 100.2323,\"stream\": [{\"id\": 1,\"values1\": 123123113245,\"values2\": 234223824733},{\"id\": 2,\"values1\": 283718378317371,\"values2\": 23823837287318}]"; | |
var fs = require('fs'); | |
var path = require('path'); | |
var response = require("fs"); | |
process.stdout.write("DIRNAME: " + __dirname); | |
var filePath = path.join(__dirname, 'document.json'); | |
//reading & writting file | |
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) | |
{ | |
if (!err) | |
{ | |
console.log("Logging this as " + data); | |
var jsonObject = JSON.parse(data); | |
console.log("This should be data value " + jsonObject.stream[0].values1); | |
saveFile("dataset\\" + "test1.json", data); | |
} else | |
{ | |
console.log(err); | |
} | |
}); | |
mongoose.connect('mongodb://localhost/medhacks'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function () | |
{ | |
process.stdout.write(" CONNECTED! "); | |
}); | |
//updating mongoDB | |
var modelRow = db.model('MedHacks', medhacksSchema); | |
modelRow.update({name: 'test1'}, {$pushAll: {json_GET}}, {upsert: true}, function (err) | |
{ | |
if (err) | |
{ | |
console.log(err); | |
} else | |
{ | |
console.log(" Successfully added "); | |
} | |
}); | |
/** | |
* Get port from environment and store in Express. | |
*/ | |
var port = normalizePort(process.env.PORT || '3000'); | |
app.set('port', port); | |
/** | |
* Create HTTP server. | |
*/ | |
var server = http.createServer(app); | |
/** | |
* Listen on provided port, on all network interfaces. | |
*/ | |
server.listen(port); | |
server.on('error', onError); | |
server.on('listening', onListening); | |
/** | |
* Normalize a port into a number, string, or false. | |
*/ | |
function normalizePort(val) | |
{ | |
var port = parseInt(val, 10); | |
if (isNaN(port)) | |
{ | |
// named pipe | |
return val; | |
} | |
if (port >= 0) | |
{ | |
// port number | |
return port; | |
} | |
return false; | |
} | |
/** | |
* Event listener for HTTP server "error" event. | |
*/ | |
function onError(error) | |
{ | |
if (error.syscall !== 'listen') | |
{ | |
throw error; | |
} | |
var bind = typeof port === 'string' | |
? 'Pipe ' + port | |
: 'Port ' + port; | |
// handle specific listen errors with friendly messages | |
switch (error.code) | |
{ | |
case 'EACCES': | |
console.error(bind + ' requires elevated privileges'); | |
process.exit(1); | |
break; | |
case 'EADDRINUSE': | |
console.error(bind + ' is already in use'); | |
process.exit(1); | |
break; | |
default: | |
throw error; | |
} | |
} | |
/** | |
* Event listener for HTTP server "listening" event. | |
*/ | |
function onListening() | |
{ | |
var addr = server.address(); | |
var bind = typeof addr === 'string' | |
? 'pipe ' + addr | |
: 'port ' + addr.port; | |
debug('Listening on ' + bind); | |
} | |
/** | |
* Saves a file | |
* */ | |
function saveFile(filename, data) | |
{ | |
var fs = require('fs'); | |
fs.writeFile(filename, data, function (err) | |
{ | |
if (err) | |
{ | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
} | |
/** | |
* Stringifies and posts a string | |
* */ | |
function post(jsonValue) | |
{ | |
var request = require('request'); | |
var stringifiedValue = JSON.stringify(jsonValue) + "\\n"; | |
request.post( | |
'http://www.yoursite.com/formpage', stringifiedValue, | |
function (error, response, body) | |
{ | |
if (!error && response.statusCode == 200) | |
{ | |
console.log(body); | |
} | |
} | |
); | |
} | |
var request = Request.prototype; | |
function Request(id, valArray) | |
{ | |
this._id = id; | |
this._val1 = valrArray[1]; | |
this._val2 = valrArray[2]; | |
this._val3 = valrArray[3]; | |
this._val4 = valrArray[4]; | |
this._val5 = valrArray[5]; | |
this._val6 = valrArray[6]; | |
this._val7 = valrArray[7]; | |
this._val8 = valrArray[8]; | |
this._val9 = valrArray[9]; | |
this._val10 = valrArray[10]; | |
this._val11 = valrArray[11]; | |
} | |
/** | |
* handling the HTTP Request | |
* Request Example: www.mydomain.com/id=120&val1=12&val2=42&val3=32&val4=12&val5=42&val6=82&val7=91&val8=81&val9=62&val10=41&val11=12 | |
*/ | |
var server = http.createServer(); | |
server.on('request', function (request) | |
{ | |
var id = request.query.id; // $_GET["id"] | |
var val1 = request.query.val1; | |
var val2 = request.query.val2; | |
var val3 = request.query.val3; | |
var val4 = request.query.val4; | |
var val5 = request.query.val5; | |
var val6 = request.query.val6; | |
var val7 = request.query.val7; | |
var val8 = request.query.val8; | |
var val9 = request.query.val9; | |
var val10 = request.query.val10; | |
var val11 = request.query.val11; | |
var valArray = [val1, val2, val3, val4, val5, val6, val7, val8, val9, val10, val11]; | |
var myRequest = Request(id, valArray); | |
//TODO save in DB and post data on the mobile | |
}); | |
method.getID = function () | |
{ | |
return this._id; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Module dependencies. | |
*/ | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var app = require('../app'); | |
var debug = require('debug')('MedHacksBackend:server'); | |
var http = require('http'); | |
var Schema = mongoose.Schema; | |
app.use(bodyParser.json()); // to support JSON-encoded bodies | |
var medhacksSchema = new Schema({ | |
duration: String, | |
stream: [{1: String, 2: String}] | |
}); | |
var json_GET = "{\"duration\": 100.2323,\"stream\": [{\"id\": 1,\"values1\": 123123113245,\"values2\": 234223824733},{\"id\": 2,\"values1\": 283718378317371,\"values2\": 23823837287318}]"; | |
var fs = require('fs'); | |
var path = require('path'); | |
var response = require("fs"); | |
process.stdout.write("DIRNAME: " + __dirname); | |
var filePath = path.join(__dirname, 'document.json'); | |
//reading & writting file | |
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) | |
{ | |
if (!err) | |
{ | |
console.log("Logging this as " + data); | |
var jsonObject = JSON.parse(data); | |
console.log("This should be data value " + jsonObject.stream[0].values1); | |
saveFile("dataset\\" + "test1.json", data); | |
} else | |
{ | |
console.log(err); | |
} | |
}); | |
mongoose.connect('mongodb://localhost/medhacks'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function () | |
{ | |
process.stdout.write(" CONNECTED! "); | |
}); | |
//updating mongoDB | |
var modelRow = db.model('MedHacks', medhacksSchema); | |
modelRow.update({name: 'test1'}, {$pushAll: {json_GET}}, {upsert: true}, function (err) | |
{ | |
if (err) | |
{ | |
console.log(err); | |
} else | |
{ | |
console.log(" Successfully added "); | |
} | |
}); | |
/** | |
* Get port from environment and store in Express. | |
*/ | |
var port = normalizePort(process.env.PORT || '3000'); | |
app.set('port', port); | |
/** | |
* Create HTTP server. | |
*/ | |
var server = http.createServer(app); | |
/** | |
* Listen on provided port, on all network interfaces. | |
*/ | |
server.listen(port); | |
server.on('error', onError); | |
server.on('listening', onListening); | |
/** | |
* Normalize a port into a number, string, or false. | |
*/ | |
function normalizePort(val) | |
{ | |
var port = parseInt(val, 10); | |
if (isNaN(port)) | |
{ | |
// named pipe | |
return val; | |
} | |
if (port >= 0) | |
{ | |
// port number | |
return port; | |
} | |
return false; | |
} | |
/** | |
* Event listener for HTTP server "error" event. | |
*/ | |
function onError(error) | |
{ | |
if (error.syscall !== 'listen') | |
{ | |
throw error; | |
} | |
var bind = typeof port === 'string' | |
? 'Pipe ' + port | |
: 'Port ' + port; | |
// handle specific listen errors with friendly messages | |
switch (error.code) | |
{ | |
case 'EACCES': | |
console.error(bind + ' requires elevated privileges'); | |
process.exit(1); | |
break; | |
case 'EADDRINUSE': | |
console.error(bind + ' is already in use'); | |
process.exit(1); | |
break; | |
default: | |
throw error; | |
} | |
} | |
/** | |
* Event listener for HTTP server "listening" event. | |
*/ | |
function onListening() | |
{ | |
var addr = server.address(); | |
var bind = typeof addr === 'string' | |
? 'pipe ' + addr | |
: 'port ' + addr.port; | |
debug('Listening on ' + bind); | |
} | |
/** | |
* Saves a file | |
* */ | |
function saveFile(filename, data) | |
{ | |
var fs = require('fs'); | |
fs.writeFile(filename, data, function (err) | |
{ | |
if (err) | |
{ | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
} | |
/** | |
* Stringifies and posts a string | |
* */ | |
function post(jsonValue) | |
{ | |
var request = require('request'); | |
var stringifiedValue = JSON.stringify(jsonValue) + "\\n"; | |
request.post( | |
'http://www.yoursite.com/formpage', stringifiedValue, | |
function (error, response, body) | |
{ | |
if (!error && response.statusCode == 200) | |
{ | |
console.log(body); | |
} | |
} | |
); | |
} | |
var request = Request.prototype; | |
function Request(id, valArray) | |
{ | |
this._id = id; | |
this._val1 = valrArray[1]; | |
this._val2 = valrArray[2]; | |
this._val3 = valrArray[3]; | |
this._val4 = valrArray[4]; | |
this._val5 = valrArray[5]; | |
this._val6 = valrArray[6]; | |
this._val7 = valrArray[7]; | |
this._val8 = valrArray[8]; | |
this._val9 = valrArray[9]; | |
this._val10 = valrArray[10]; | |
this._val11 = valrArray[11]; | |
} | |
/** | |
* handling the HTTP Request | |
* Request Example: www.mydomain.com/id=120&val1=12&val2=42&val3=32&val4=12&val5=42&val6=82&val7=91&val8=81&val9=62&val10=41&val11=12 | |
*/ | |
var server = http.createServer(); | |
server.on('request', function (request) | |
{ | |
var id = request.query.id; // $_GET["id"] | |
var val1 = request.query.val1; | |
var val2 = request.query.val2; | |
var val3 = request.query.val3; | |
var val4 = request.query.val4; | |
var val5 = request.query.val5; | |
var val6 = request.query.val6; | |
var val7 = request.query.val7; | |
var val8 = request.query.val8; | |
var val9 = request.query.val9; | |
var val10 = request.query.val10; | |
var val11 = request.query.val11; | |
var valArray = [val1, val2, val3, val4, val5, val6, val7, val8, val9, val10, val11]; | |
var myRequest = Request(id, valArray); | |
//TODO save in DB and post data on the mobile | |
}); | |
method.getID = function () | |
{ | |
return this._id; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment