Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sapics/request-country
Get 2-letter country code from http request in nodejs
https://github.com/sapics/request-country
Last synced: 3 months ago
JSON representation
Get 2-letter country code from http request in nodejs
- Host: GitHub
- URL: https://github.com/sapics/request-country
- Owner: sapics
- Created: 2016-06-15T02:59:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-03-14T15:29:49.000Z (almost 3 years ago)
- Last Synced: 2024-09-10T11:19:35.987Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 8
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# request-country [![NPM version](https://badge.fury.io/js/request-country.svg)](https://badge.fury.io/js/request-country)
Get 2-letter country code from http request in nodejs.
## Installation
```
npm i request-country
```## Usage
```javascript
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](https://en.wikipedia.org/wiki/Private_network) IPs, you can pass second argument. So if you open your site from private network or localhost it will be returned.
```javascript
requestCountry(req, 'US');
```You can get country code from ip address string as `requestCountry('2.2.2.2')`.
### As Connect Middleware
```javascript
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](https://github.com/sapics/node-geoip-country) for getting country code.
[geoip-country](https://github.com/sapics/node-geoip-country) includes GeoLite2 ipv4 and ipv6 country data which created by MaxMind, available from https://www.maxmind.com.
Thus, the license of this library is based on the GeoLite2 License. See the [LICENSE](https://github.com/bluesmoon/node-geoip/blob/master/LICENSE) file for details.