request-country

Get country code from http request in nodejs

Usage no npm install needed!

<script type="module">
  import requestCountry from 'https://cdn.skypack.dev/request-country';
</script>

README

request-country NPM version

Get 2-letter country code from http request in nodejs.

Installation

npm i request-country

Usage

var http = require('http');
var requestCountry = require('request-country');
var server = http.createServer(function(req, res) {
  console.log(requestCountry(req));
  // If it cannot detect country code from request ip,
  // the function return false.
});

If you want to set default value for private network IPs, you can pass second argument. So if you open your site from private network or localhost it will be returned.

  requestCountry(req, 'US');

You can get country code from ip address string as requestCountry('2.2.2.2').

As Connect Middleware

var requestCountry = require('request-country');
app.use(requestCountry.middleware({
  attributeName: 'requestCountryCode', // default is 'requestCountryCode'
  privateIpCountry: 'US' // Result for private network IPs
}));

app.use(function(req, res) {
  res.end(req.requestCountryCode);
});

LICENSE

This library use the geoip-country for getting country code. Thus, the license of this library is based on that. See the LICENSE file for details.