How to detect if an app is running with Cloud9? - node.js

I'm developping a node application, sometimes with Eclipse (locally) and sometimes with Cloud9 (remotely). As I'm using a database, I have one local with mongoDB and another remote on mongoHQ.
If I dont want to use the DB on mongoHQ when I work locally, how could I tell my nodejs application (understand automatically detect) that my IDE is Cloud9 or another one ? I could use a simple local variable that I change each time I change IDE, but is it possible to do it automatically ?

One way to detect cloud9-ide is to check if C9_PROJECT environment variable is set:
if (process.env.C9_PROJECT) {
console.log('Running on C9 IDE');
} else {
// ...
}
There are also other variables prefixed with C9. You can check them if you want to.

Related

Node-Red mongodb3 connect DB using URL from environment variable

I'm running Node-Red embedded in Express application. Also using 'dotenv' to load environment variables.
For storage using MongoDB with 'node-red-contrib-mongodb3'.
Everything works as expected. But, I have different environments and different MongoDB for each environments.
I want to connect to MongoDB from configuration (.env file or environment file).
Something like, in MongoDB config node URL input box golbal.get('env').MONGODB_DEV_URL or msg.MONGODB_URL
Tried looking for an option in the documentation of 'mongodb3' and google, still no luck. Any help or direction will be appreciated.
From the Node-RED docs
Any node property can be set with an environment variable by setting
its value to a string of the form ${ENV_VAR}. When the runtime loads
the flows, it will substitute the value of that environment variable
before passing it to the node.
This only works if it replaces the entire property - it cannot be used
to substitute just part of the value. For example, it is not possible
to use CLIENT-${HOST}.

Changing Express constant based on URL (ie localhost vs production)

I'm fairly new to node and express...
I have a constant I set in my Node/Express application for "Domain". I'm constantly switching between localhost and the actual url when I switch between development and production.
I'm trying to figure out how I can automatically test the URL so that when the code is read on the server (ie actual domain url vs localhost) that it sets the proper domain url.
Here is my code:
function define(name, value) {
Object.defineProperty(exports, name, {
value: value,
enumerable: true
});
}
define("DOMAIN", "http://www.actual_domain.com");
// define("DOMAIN", "http://localhost:3000");
Anyone have a simple solution for this?
Thanks!!
there is many solutions for this, but usually this is done by environment variables, depends on your platform, but, in Linux systems, you do the following in your shell
export ENV_URL="http://www.example.com"
and to make sure its exported successfully
echo $ENV_URL
and in your code you do
const base_url = process.env.ENV_URL || "http://www.default.com";
in your local machine you set the ENV_URL to localhost or whatever you prefer, and in your server you set it to your actual URL.
or you could simply have many configuration files and you can determine the appropriate one by the environemnt variable like
export ENV=PROD
and in your code you can load the prod configuration file that contains your environment configurations.
The de facto way to do this sort of thing is through environment variables. Have a look at 12factor.net for a load of best practices, but in general you want to put differences in the environment into environment variables, then read those variables at runtime.

Meteor server side remote debugging

Versions
I'm using Meteor 1.0.3 and node 0.10.35 on a small Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-3d50120d EC2 instance.
Context
I know how to do server side debugging on my development box, just $ meteor debug and open another browser pointing to the url it produces -- works great.
But now, I'm getting a server error on my EC2 instance I'm not getting in development. So I'd like to set up a remote debug session sever side.
Also, I deployed to the EC2 instance using the Meteor-up package (mup).
EDIT
In an effort to provide more background (and context) around my issue I'm adding the following:
What I'm trying to do is, on my EC2 instance, create a new pdf in a location such as:
application-name/server/.files/user/user-name/pdf-file.pdf
On my OSX development box, the process works fine.
When I deploy to EC2, and try out this process, it doesn't work. The directory:
/user-name/
for the user is never created for some reason.
I'd like to debug in order to figure out why I can't create the directory.
The code to create the directory that works on my development box is like so:
server.js
Meteor.methods({
checkUserFileDir: function () {
var fs = Npm.require('fs');
var dir = process.env.PWD + '/server/.files/users/' + this.userId + '/';
try {
fs.mkdirSync(dir);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}
}
});
I ssh'd into the EC2 instance to make sure the path
/server/.files/user/
exists, because this portion of the path is neccessary in order for the above code to work correctly. I checked the path after the code should have ran, and the
/user-name/
portion of the path is not being created.
Question
How can I debug remote server side code in a easy way on my EC2 instance, like I do on my local development box?
Kadira.io supports remote errors/exceptions tracking. It allows you to see the stacktrace on server side exceptions in the context of your meteor methods.
See https://kadira.io/error-tracking.html for more detail.
It seems in my case, since I'm using Meteor-up (mup), I can not debug per-say, but get access to the remote EC2 instance server console and errors by using command $ mup logs -f on my development box.
This effectively solves my issue with being blind on the server side remote instance.
It still falls short of actual debugging remotely, which speeds up the process of finding errors and performance bottlenecks, but it's all we have for now.
For someone who still searching:
#zodern added server-side debugging of meteor apps to great meteor-up tool:
https://github.com/zodern/meteor-up/pull/976
Do mup meteor debug in deployment dir and you will be almost set, just follow the text.

How can I set up a node app (ember app kit) on heroku that reads ENV variables and makes the values available to the application?

Okay, I'm new to node, and really only just using the node server to serve static js, but I can't find any info on this anywhere.
I'm running an application ember app kit, which gets built to a node server.js for deploy, and heroku runs it with node server.js.
It uses grunt for building, testing, etc.
I'd like to know how I can specify configuration variables (i.e. authentication tokens) that can be overridden by heroku config variables.
The closest I've been able to get is a custom task that reads environment variables and writes out a json file that gets built into the site (and assigned to a global var). This works locally, but doesn't take into account heroku configs.
I even wrote a deploy script that gets heroku's configs, exports them as environment variables locally, and does the build--Which works, but the configs only get updated on app deploy. So if I do a heroku config:add CONFIG_TEST=test_value, my app doesn't see that value for CONFIG_TEST until the next time I deploy the app.
I'd like for my app to start embedding that config value in the browser JS immediately.
Any way to do this with node the way my app is set up?
I am not sure I understand what's wrong with simply taking config variables, at run time, from the environment. Use process.env.KEY in your code, and embed that result into whatever template you may have, and serve that as the result.
When you change Heroku config variables your process gets restarted, so it picks up the new values.
Is the problem the fact that you serve static files? If so -- can you simply change it so that you use a template engine to do some processing on them before serving?
OK, here's a solution for ember-app-kit using grunt-sed.
In EMBER_APP_KIT_PROJECT/tasks/options/sed.js
Add something like
module.exports = {
version: {
path: "./dist/",
pattern: '{{env.API_BASE_PATH}}',
replacement: function(){
return process.env.API_BASE_PATH;
},
recursive: true
}
};
then in your code just put
"{{env.API_BASE_PATH}}"
Now, when you run
$ grunt sed
it will replace "{{env.API_BASE_PATH}}" with whatever's in the environment variable.

Change configuration in runtime by changing environment variables using the module node-config

I'm trying to use the node-config module to change some parameters of my configuration (basically logging level) during runtime.
In the official documentation says:
Environment variables can be used to override file configurations. Any environment variable that starts with $CONFIG_ is set into the CONFIG object.
I've checked that this is true when the server starts but it does not seem to work once it's up. (The handler of the watch function is never called when an environment variable is changed unlike a change in the runtime.json file or directly changing a config variable).
I'm currently watching the whole CONFIG object like this:
var CONFIG = require('config');
CONFIG.watch( CONFIG , null , function(object, propertyName, priorValue, newValue){
console.log("Configuration change detected");
});
Does anyone know if this is possible?
The environment is available during startup of a process.
If the process is running, you won't be able to change the environment anymore, the process is in.
The only option is to restart the process or use other mechanisms to communicate with it.
Say for example having a rest or tcp listener inside, where you can transfer your variable inside.
Best regards
Robert
As you must knowing, React is a single page application which is eventually when it is complied is a static page app that means all the files of the react application is complied into vanilla JS and CSS file bundle in a Tarball. Now that Tarball is eventually deployed on a web server. It could be Apache web server, nginx web server or anything which you are using it but an important point is the static app is running in someone else browser and someone access to website CSS and JS are downloaded in a browser and it is running in the browser runtime environment so technically you cannot have a runtime environment variable for someone else browser but may be there would be a way to access them during runtime.
SOLUTION
I have achieved this goal with the package called runtime-cra.
follow the steps on this official documentation: https://blog.risingstack.com/create-react-app-runtime-env-cra/

Resources