diff --git a/binding/nodejs/.gitignore b/binding/nodejs/.gitignore index c2658d7..902b281 100644 --- a/binding/nodejs/.gitignore +++ b/binding/nodejs/.gitignore @@ -1 +1,2 @@ node_modules/ +data/ diff --git a/binding/nodejs/README.md b/binding/nodejs/README.md index dbaa6c0..a5d00c7 100644 --- a/binding/nodejs/README.md +++ b/binding/nodejs/README.md @@ -1,5 +1,18 @@ # nodejs 客户端 +## Install +``` +npm install lionsoul-ip2region --save +``` + +## Example + +``` +const searcher = require('lionsoul-ip2region').create(); +searcher.btreeSearchSync('xxx.xxx.xxx.xxx') +// => { city: 2163, region: '中国|0|广东省|深圳市|联通' } +``` + ## 实现情况: 现已实现同步和异步查询,具体使用方法可以参考 `nodejs\tests\constructorTest.spec.js` 和`nodejs\tests\createTest.spec.js`。 diff --git a/binding/nodejs/deploy.sh b/binding/nodejs/deploy.sh new file mode 100644 index 0000000..ddc7587 --- /dev/null +++ b/binding/nodejs/deploy.sh @@ -0,0 +1,11 @@ +#!/bin/sh +echo 'create dir' +if [ ! -d "data" ]; then + mkdir data +fi + +echo "copy db" +cp '../../data/ip2region.db' './data/' + +echo "npm publish" +npm publish diff --git a/binding/nodejs/ip2region.js b/binding/nodejs/ip2region.js index 0a7d19e..b77b5d6 100644 --- a/binding/nodejs/ip2region.js +++ b/binding/nodejs/ip2region.js @@ -8,6 +8,8 @@ * @author dongwei */ const fs = require('fs'); +const path = require('path'); +const DEFAULT_DB_PATH = path.join(__dirname,'./data/ip2region.db') ; //#region Private Functions /** @@ -134,7 +136,7 @@ class IP2Region { //#region Static Functions // Single Instance - static create(dbPath) { + static create(dbPath = DEFAULT_DB_PATH) { let existInstance = _globalInstances.get(dbPath); if (existInstance == null) { existInstance = new IP2Region({ dbPath: dbPath }); diff --git a/binding/nodejs/package.json b/binding/nodejs/package.json index 4cd9e86..fe8bee7 100644 --- a/binding/nodejs/package.json +++ b/binding/nodejs/package.json @@ -1,5 +1,5 @@ { - "name": "ip2region", + "name": "lionsoul-ip2region", "version": "0.0.1", "description": "ip database ", "main": "ip2region.js", @@ -7,6 +7,7 @@ "test": "jest", "coverage": "npm run test && jest --coverage" }, + "files": ["data/"], "repository": { "type": "git", "url": "https://github.com/lionsoul2016/ip2region" @@ -26,4 +27,4 @@ "jest": "^19.0.2", "benchmark": "^2.1.4" } -} +} \ No newline at end of file