简体   繁体   中英

How can I get the server name for where node.js is running?

I want to add a line that will point to a different database depending on the server I am running node.js on (localhost vs test.com, etc). How do I get the server name in node.js? That is, what is the server side equivalent to location.host?

var os = require('os');
os.hostname();

link

req.headers.host #The request host.

你可以在这里参考 Node.js 文档。

I have the same question: How to get the server name/FQDN from node.js? I came across get-fqdn on NPM.

npm -i get-fqdn --save

const getFQDN = require('get-fqdn');
// async/await usage
(async () => {
  try {
    const fqdn = await getFQDN();
    console.log('fqdn', fqdn);
  } catch (err) {
    console.log(err);
  }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM