nuip
nuip
is a lightweight Node.js package that fetches user location details based on their IP address. It uses ip-api.com
to retrieve the data and supports both real and development modes.
📞 Installation
Install nuip
using npm:
npm install nuip
If you're using TypeScript, you might also want to install the necessary types:
npm install --save-dev @types/node
🚀 Usage
Import the Package
You can import getNuip
in both JavaScript and TypeScript:
import { getNuip } from "nuip";
🔍 Fetching IP Information
Basic Usage
import { getNuip } from "nuip";
async function fetchIpInfo() {
const ipInfo = await getNuip();
console.log(ipInfo);
}
fetchIpInfo();
By default, this will return the IP details of the requesting client.
🌍 Using in an Express Server
If you're using Express, you can create an API endpoint:
import express, { Request, Response } from "express";
import { getNuip } from "nuip";
const app = express();
const port = 3000;
app.get("/getNuip", async (req: Request, res: Response) => {
try {
const ipInfo = await getNuip(req);
res.json(ipInfo);
} catch (error) {
res.status(500).json({ error: "Failed to fetch IP info" });
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
⚙️ Advanced Options
You can pass an options object to getNuip
:
const ipInfo = await getNuip(req, { devMode: true, includeDetails: false });
Options:
Option | Type | Default | Description |
---|
req | Request | - | (Optional) Express request object to get real client IP. |
devMode | boolean | false | If true , returns mock data (0.0.0.0 ). |
includeDetails | boolean | true | If false , fetches only basic details. |
📝 Response Format
By default, getNuip
returns an object like this:
{
"devMode": false,
"ip": "0.0.0.0",
"continent": "Unknown",
"continentCode": "XX",
"country": "Unknown",
"countryCode": "XX",
"region": "X",
"regionName": "Unknown Region",
"city": "Unknown City",
"district": "",
"zip": "0000",
"lat": 0.0,
"lon": 0.0,
"timezone": "UTC",
"offset": 0,
"currency": "XXX",
"isp": "Unknown ISP",
"org": "Unknown Organization",
"as": "AS00000 Unknown ASN",
"asname": "UNKNOWN-ASN"
}
If devMode: true
, it returns mock data:
{
"devMode": false,
"ip": "0.0.0.0",
"continent": "Unknown",
"continentCode": "XX",
"country": "Unknown",
"countryCode": "XX",
"region": "X",
"regionName": "Unknown Region",
"city": "Unknown City",
"district": "",
"zip": "0000",
"lat": 0.0,
"lon": 0.0,
"timezone": "UTC",
"offset": 0,
"currency": "XXX",
"isp": "Unknown ISP",
"org": "Unknown Organization",
"as": "AS00000 Unknown ASN",
"asname": "UNKNOWN-ASN"
}
⚖️ Development Mode
When running in a development environment, you can use:
const ipInfo = await getNuip(null, { devMode: true });
console.log(ipInfo);
This will return mock IP data (0.0.0.0
) instead of making an API request.
🗃️ License
This project is licensed under the MIT License.
👯️ Contact
For questions or suggestions, feel free to open an issue on GitHub.