How to see process.env after starting server - node.js

I am developing sample apps and would like to know process.env variables,
I know console.log(process.env) will return its variables.
But can I see them after run its server ?
npm start
I couldn't input anything in console.
As I am new to node.js, will you please let me know.
by switing NODE_ENV, it seems that development,staging,production is switched.
So that I would like to comfirm them.
Thanks

If you start your server in docker and don't pass custom variables to process.env in your app, you can see your env by docker command:
docker exec your_container env
Yet another way - create a specific route in your application that will be return you all data from process.env.
Something like this:
GET yourserver/api/system/env
But this way is not secured and you should think about protection of your system route.
UPD
Also you can call console.log(process.env) after server has been started.
await app.listen(3000);
console.log(process.env);

Related

How to extract secrets from running node app via debugger

I'm trying to hack my own node app and extract secrets from it by attaching a debugger to the running process. It's a NestJS / Express app.
So there is a compiled main.ts file of the same form as here running.
I am attaching a debugger via node inspect -p $PID. I can now go into a repl, but from there I don't know how to access the instantiated NestJS objects...
There is a ConfigService class from which I would like to retrieve the variables.
How would I continue here?

Accessing user uploaded images in a deployed MERN app

I am a relatively new developer and I have made a personal blog app, where a user can create a post and upload an image to use as the thumbnail for that post.
So far the backend and frontend work brilliantly and I am able to get the image, store it locally in a folder on my machine, store the file path in MongoDB and then access it and display it in the UI accordingly.
Now that I'm looking to finally deploy my application I have to figure out a way to upload images to an online cloud storage or something, where I can access them from my frontend as well.
Any suggestions on a good service of this kind and if possible, something free, suitable for my small project? Or any suggestions on an alternative way of dealing with this situation?
Any advice will be greatly appreciated!
Thanks in advance!
NOTE: I plan on deploying my app with Heroku, so if you've ever dealt with this issue directly using Heroku, please share your experience.
Yes, I have several apps that do just this! Sign up for a free MongoDB Atlas account and then you can store the data on their servers and point your Express app to the connection URL. Basically, they will give you a URL like this:
mongodb+srv://your-cluster-name:a232dfjoi39034r#atlas-free-cluster-czaoo.mongodb.net/blog-app?retryWrites=true
Which you can then store in a .env file like this:
MONGODB_URL=mongodb+srv://your-cluster-name:a232dfjoi39034r#atlas-free-cluster-czaoo.mongodb.net/blog-app?retryWrites=true
And access from your app like so:
mongoose.connect(process.env.MONGODB_URL, connectionOptions)
.catch((err) => {
console.log('Error on initial DB connection: ', err);
});
You'll need to load the .env files in your app using an npm packages such as dotenv on the development side. For heroku, you can use the heroku-cli
and set any environment variables for your app like so:
heroku config:set MONGODB_URL=mongodb+srv://your-cluster-name:a232dfjoi39034r#atlas-free-cluster-czaoo.mongodb.net/blog-app?retryWrites=true
Note, the development MongoDB URL could be a connection string to a local instance, such as:
MONGODB_URL=mongodb://localhost:27017/my-blog-app
And the one for heroku can be the MongoDB Atlas cluster.
There are a few other config things to do for Node apps, like having a 'start' script in package.json, which for Express apps created with express-generator looks like:
"scripts": {
"start": "node ./bin/www"
But for your case may be different. It should point to whatever file is the entry point for your server.
You'll also need to set the PORT from process.env.PORT, which heroku will set on their end, and on your end you can just default to 3000:
const PORT = process.env.PORT || 3000;
app.listen(PORT);
If you have any questions, feel free to PM me or just ask here and I can elaborate further. It can be intimidating the first few times, but it's actually really easy to deploy stuff to heroku this way once you get the hang of it! You can also check out the heroku docs for deploying Node apps.

process.env.PORT is undefined (in LinuxCloud environment)

I've been developing a nodejs app on C9 for some time and now I'm trying to make a copy of it on my remote host. So far, in the new environment node app.js works in the console but I am unable to view the website in my browser.
It seems that it is a port issue.
My app.js file goes like this :
var express = require("express"),
app = express();
(...)
app.listen(process.env.PORT, process.env.IP, function(){
console.log(process.env.PORT);
console.log("The YelpCamp Server Has Started!");
});
In the C9 environment, the log tells me that process.env.PORT is 8080. But in the new environment, the log tells me that process.env.PORT is undefined.
How can I fix this ?
This is similar to this older question except that my remote OS is Linux not Windows. The answer to this question says that one should "modify the web.config file", but I couldn't find it on my remote host and I'm not sure it works the same way under Linux and under Windows.
process.env variables are set by the "Environment". Basically, they act as access to your systems environment variables. When trying to access process.env.PORT it'll return undefined if you've not setup that environment variable in the shell that you're trying to run your system.
You can set the environment variable up before you run node app.js with the following.
$ PORT=8080
$ node app.js
In this case we set the environment variable within the existing shell, then we call node with the app.js file. Environment variables are passed from parent processes into processes they start, if you run those two in sequence, you'll be setting up an environment variable within the current shell, and node will then receive that when it starts up (along side all other environment variables).
To see all environment variables avalible to the existing node process you can run console.log(process.env);.
This will be the same for process.env.IP as well.
Side note: C9 and other similar environments often have a lot of pre-set environment variables. This is why it was available on C9. The same is true for Heroku as well, this is because their system must dictate the port your service should use so that their load balancers / reverse proxies can be pre-configured for that port.
I ran into something similar and ended up providing a default port number if one couldn't be read from the environment.
const PORT = process.env.PORT || 8080;
app.listen(PORT, ...)
After deploying your node project files, you'll need to make sure any env variables are also transferred.
1. Manually copy-paste env variables
From the original host config to the new host config. Use the dashboards on either end. Simple and secure.
--- or ---
2. Manually transfer .env file from project root
(Not recommended for production environments) Node projects often have a .env file in the root, which is commonly excluded from file transfers (for obvious security reasons). You'd need to make sure the new host also has the identical .env file in root. For production environments, this is usually not desired for security reasons, and it's better to use option #1.
Projects using this method are likely using the dotenv package which detects .env and makes the parameters available to your app. It's mainly for development convenience.

Console.log in a production app using node.js, express and PM2

I'm using console.log for debugging some information in my web app using node.js, expressjs and PM2. All is working fine :)
I want to know if my web app can have performance problems in production if I use console.log this:
var myVariable = "Enter in this section of code";
console.log(myVariable);
I have read about console.log is synchronous and can affect to my performance... if this is real in this moment (I'm using Express 4), how can I remove these console.log in different environments like production?
I'm using PM2 (pm2 logs command) to watch the logs and pm2 flush to clean the logs.
Thanks.
You could use winston for logging , you could configure transport depending on your env , for example you could use Console and File tranports in development and just File in production. PD: pm2 takes data from std log(console)

How can I access to my nodeJS app in console on my server?

I have an application nodeJS started like a deamon with forever(https://github.com/nodejitsu/forever).
So now, how can I access to my app and be abble to watch and change values of variables with SSH connexion on my server?
How open console on my app without interuption of service?
thx.
You should give node-inspector a look.
There's nothing built-in for that.
However, you can create a function on the server that will eval whatever string you put as parameter.
To query that function, you can use ajax or socket.io for example.
Socket.io example
socket.on('evalStr', function (str) { eval(str);}); //server
socket.emit('evalStr', "myVar = 10;"); //client via console
Edit: Of course, you can add security such as testing a password before evaluating.

Resources