I am needing to check if my node js app using ExpressJS is running on my localhost (Development env.) and not on the live server and execute certain code if so. This has to be done server side. Any ideas how to check this?
I am needing to check if my node js app using ExpressJS is running on my localhost (Development env.) and not on the live server and execute certain code if so. This has to be done server side. Any ideas how to check this?
Share Improve this question asked Oct 12, 2017 at 12:31 Stefan ZeuchStefan Zeuch 1411 gold badge2 silver badges12 bronze badges 2-
You'll probably get answers telling you to look at
req.hostname
. Don't. It's derived from theHost
header, which is trivially spoofed. – T.J. Crowder Commented Oct 12, 2017 at 12:40 -
One option is to check the path variable
__dirname
. Check to see if it matches the server path. You can also add a file to the server directory that is not in your localhost directory. If it exists (or doesn't) you can check for that. – 1.21 gigawatts Commented Nov 4, 2023 at 3:20
1 Answer
Reset to default 7Pass process.env variable to your node.js instance like this (for example)
node -e 'process.env.NODE_ENV = "development"'
and then just use
if(process.env.NODE_ENV === 'development') {
...
}