-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapi.js
54 lines (49 loc) · 1.51 KB
/
api.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require('express')
const fetch = require('node-fetch')
const jsdom = require("jsdom")
const { JSDOM } = jsdom
const app = express()
app.get('/',(req,res) => {
res.status(200).send("api/{bin}")
})
app.get('/api/:bin', (req, res)=> {
const bin = req.params.bin
const api = `https://bins.ws/search?bins=${bin}`;
try{
fetch(api)
.then( response => response.text())
.then(data => {
data = new JSDOM(data);
binData = data.window.document.querySelector(".page tbody tr").textContent
let binInfo = binData.split("\n")
binInfo = binInfo.filter(i=>i)
let binObject = {
result: true,
bin : binInfo[0],
type: binInfo[1],
level: binInfo[2],
brand: binInfo[3],
bank: binInfo[4],
country: binInfo[5]
}
res.send(binObject)
})
.catch(error => {console.error('Error:', error)
let object = {
result: false,
message: "Invalid BIN"
}
res.status(200).send(object)
})
}
catch(error){
let object = {
result: false,
message: error.message
}
res.send(object)
}
})
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});