-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (30 loc) · 1.07 KB
/
server.js
File metadata and controls
41 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// importing the required packages
const express = require('express');
const server = express();
const fetch = require('node-fetch');
require('dotenv').config();
// initializing and setting server up
server.use(express.static('public'));
server.use(express.json());
// getting request from clientside and sending data back from the fetch results
server.get('/resource/:Addr', (req, res) => {
const user = req.params;
fetch('https://geo.ipify.org/api/v2/country,city?apiKey='+ process.env.ACCESS_KEY +'&ipAddress=' + user.Addr)
.then((reply) => reply.json())
.then((ans) => {
let data = {
ip: ans.ip,
location: ans.location,
isp: ans.isp
};
res.json(data);
});
});
// local testing environment
if (process.env.NODE_ENV !== 'production') {
server.listen(process.env.PORT || 0, () => {
console.log(`Server running on port ${process.env.PORT || 0}`);
});
}
// export app for vercel/production environment
module.exports = server;