Guest User

Untitled

a guest
Jun 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. const express = require('express');
  2. const myScanner = require('./myScanner.js');
  3. const ipGeolocation = require('./ipGeolocation');;
  4.  
  5.  
  6. var app = express();
  7.  
  8. app.use(function(req, res, next) {
  9. res.header("Access-Control-Allow-Origin", '*');
  10. res.header("Access-Control-Allow-Credentials", true);
  11. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  12. res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
  13. next();
  14. });
  15.  
  16. const port = process.env.PORT || 3000;
  17.  
  18. app.get('/', (req, res) => {
  19.  
  20. res.send('<h2>server is up</h2><br><strong>GET</strong> /api/port/:ipAddr<br><strong>GET</strong> /api/geo/:ipAddr');
  21.  
  22. });
  23.  
  24. app.get('/api/port/:ipAddr', (req, res) => {
  25.  
  26. const ip = req.params.ipAddr;
  27.  
  28. myScanner.portScan(ip)
  29. .then((list) => {
  30. res.send(list);
  31. }).catch((err) => {
  32. res.status(400).send(err); //invalid ip address
  33. });
  34.  
  35. });
  36.  
  37. app.get('/api/geo/:ipAddr', (req, res) => {
  38.  
  39. const ip = req.params.ipAddr;
  40.  
  41. ipGeolocation.locate(ip)
  42. .then((info) => {
  43. res.send(info);
  44. }).catch((err) => {
  45. res.status(400).send(err); //invalid ip address
  46. });
  47.  
  48. });
  49.  
  50. app.listen(port, () => {
  51.  
  52. var time = new Date().toString();
  53. console.log(`[${time}] Server in up on ${port}`);
  54.  
  55. });
Add Comment
Please, Sign In to add comment