Advertisement
Guest User

Untitled

a guest
Jan 6th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3. var fs = require('fs');
  4. var http = require('http');
  5. var https = require('https');
  6. var path = require('path');
  7. var axios = require('axios');
  8. //var cors = require('cors');
  9.  
  10. //app.use(cors());
  11.  
  12. app.use(function(req, res, next) {
  13.   if (req.secure) {
  14.       next();
  15.   } else {
  16.       res.redirect('https://' + req.headers.host + req.url);
  17.   }
  18. });
  19.  
  20. const httpsAgent = new https.Agent({
  21.    rejectUnauthorized: false,
  22.    cert: fs.readFileSync('sec/kbadower.net.crt')
  23.  })
  24.  
  25. app.use(express.static(__dirname + '/public'));
  26.  
  27. app.get('/', function(req, res) {
  28.   res.sendFile(path.join(__dirname + 'public/index.html'));
  29. });
  30.  
  31. app.get('/rest', function(req, res) {
  32.   axios.get('https://kbadower.net:3000/basic' , { httpsAgent, auth: { username: 'qwer', password: '1234'}}) // lab
  33.   .then(function(response){
  34.     console.log(response.data)
  35.     res.send(response.data)
  36.   })
  37.   .catch(function(e) { console.log(e); res.write(JSON.stringify(e)); })
  38. });
  39.  
  40. app.post('/rest', function(req, res) {
  41.   axios.post('https://kbadower.net:3000/basic', {   // lab
  42.     a : "44",
  43.   },  { httpsAgent })
  44.   .then(function (response) {
  45.     res.send(response.data)
  46.   })
  47.   .catch(function(e) { console.log(e); res.write(JSON.stringify(e)); })
  48. });
  49.  
  50. var key = fs.readFileSync('sec/kbadower.org.key');
  51. var cert = fs.readFileSync('sec/kbadower.org.crt');
  52.  
  53. var options = {
  54.   key: key,
  55.   cert: cert
  56.   };
  57.  
  58. https.createServer(options, app).listen(443)
  59. http.createServer(app).listen(80);
  60.  
  61. console.log(`Router.js listening on ports 443(https) and 80(http)!`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement