개발자의뇌

node 사용하여 ip to location 서버 만들기 본문

개발/Linux

node 사용하여 ip to location 서버 만들기

devbrain 2021. 4. 12. 17:54
# CentOS7 nvm 설치 node version manager
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

# nvm 설치 후 reboot
reboot

# nvm 으로 노드 설치 버전 목록 확인
nvm ls-remote


# nvm 특정 버전 설치
nvm install 14.16.1


# nvm 특정 버전 사용
nvm use 14.16.1

# node 버전 확인
node --version


# ip-to-locatino 모듈 설치
npm install ip-to-location --save


# express 모듈 설치
npm install express --save


# vi svr.js
const express = require('express');
var ip2location = require('ip-to-location');

const app = express();


app.get('/ip2location', (req, resp) => {
  var srcIP = req.query.src_ip;
  ip2location.fetch(srcIP, function(err, res){
//    console.log(res);
    resp.send(res);
  });
});

app.get('/error', (req, resp) => {
  resp.status(404).send('404 ERROR');
});

app.listen(3000, () => {
  console.log('Server is running port 3000!');
});



# 서버 실행
node svr.js


# 동작확인
curl "http://127.0.0.1:3000/ip2location?src_ip=8.8.8.8"
Comments