javascript - Express JS - Using server side JS check if app is running on localhost - Stack Overflow

I am needing to check if my node js app using ExpressJS is running on my localhost (Development env.) a

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 the Host 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
Add a ment  | 

1 Answer 1

Reset to default 7

Pass 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') {
    ...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745407423a4626376.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信