1

I have a server running with Node.js and my question is, whether it's possible when running the server like I usually do (with the command node app.js) to pass parameters (eg. [UserID; IterationID;ProfileID]). Later I want to use these parameters to generate canvas (which I'm not sure how to read the parameters).

var fs = require('fs');
const log=require('simple-node-logger').createSimpleLogger();
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var port = process.env.PORT || 8000;

app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));

app.use('/images', express.static(__dirname +'/images'));

app.get('/', function(req, res){
    res.sendfile('main.html');
});

app.listen(port, function(){
    //console.log('server is running on ' + port);
});

app.post('/submit', function(req, res){
 console.log(req.body.rank);

 return res.sendfile('success.html');
});

Thank you very much in advance!

Pepka
  • 93
  • 1
  • 9

1 Answers1

3

You can pass the environment parameters. Here is linux terminal command example:

YOUR_PARAM=param_value YOUR_PARAM2=param_value2 node app.js

Inside the code you can access those params inside process.env object:

console.log(process.env.YOUR_PARAM); // "param_value"
console.log(process.env.YOUR_PARAM2); // "param_value2"

This is usually done to define where application is running (local, development server, production server). In my opinion it is the best to put the rest of the configuration in the JSON files and load them according to the application environment. So basically first you define where your app is running and then based on that load the correct configurations from specified file. That way you can even share the configuration with the rest of the team over git.

P.S. It is also worth mentioning that convention is to define process.env variables with capital letters in order to avoid overwriting some of the nodejs or system environment variables (if you console.log the process.env object you will see lot of configuration data in there).

Goran
  • 3,292
  • 2
  • 29
  • 32