最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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') {
    ...
}
发布评论

评论列表(0)

  1. 暂无评论