juci-local-server: Add config with port option

Easy configuration object based on JSON to easily change port number.
This commit is contained in:
Stefan Nygren 2015-11-17 16:22:31 +01:00
parent c6e23a97c5
commit ba71ab809e
2 changed files with 29 additions and 4 deletions

View File

@ -6,12 +6,34 @@ var JSON = require("JSON");
var fs = require("fs");
var request = require("request");
var http = require("http");
var bodyParser = require('body-parser')
var configuration ={};
var config = {
};
};
try {
//check if file exists and is a file
if (fs.lstatSync("juci-local-server.config").isFile())
{
configuration = JSON.parse(fs.readFileSync("juci-local-server.config"));
if ( configuration.serverPort == undefined) {
throw new Error('ENOENT');
}
}
else
{
throw new Error('ENOENT');
}
}
catch (e) {
if (e.code = 'ENOENT')
{
configuration = JSON.parse('{ "serverPort" : "3000" }');
} else {
throw(e)
}
}
console.log(configuration.serverPort)
for(var i = 0; i < process.argv.length; i++){
switch(process.argv[i]){
case "--host": config.ubus_uri = "http://"+process.argv[++i]+"/ubus"; break;
@ -303,7 +325,7 @@ app.post('/ubus', function(req, res) {
*/
});
var server = app.listen(3000, function () {
var server = app.listen(configuration.serverPort, function () {
var host = server.address().address;
var port = server.address().port;

View File

@ -0,0 +1,3 @@
{
"serverPort" : 3000
}