lompo777

Miguel API nodeJS

Jun 16th, 2021
715
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. // var   bodyParser = require('body-parser');
  3. var app = express();
  4. var fs = require("fs");
  5.  
  6. // parse application/x-www-form-urlencoded
  7. //app.use(bodyParser.urlencoded({ extended: false }))
  8.  
  9. // parse application/json
  10.  
  11. var app = express();
  12.  
  13. const bodyParser = require("body-parser");
  14. app.use(bodyParser.json());
  15.  
  16.  
  17.  
  18. app.post('/', function(request, response){
  19.      
  20. console.log(request.body);
  21.  
  22.  
  23.  var amqp = require('amqplib/callback_api');
  24. amqp.connect('amqp://miguel:miguel123@152.228.133.222:32777/mobile_app', function(error0, connection) {
  25.   if (error0) {
  26.     throw error0;
  27.   }
  28.   connection.createChannel(function(error1, channel) {
  29.     if (error1) {
  30.       throw error1;
  31.     }
  32.     var exchange = 'crowd_sensing_exchange';
  33.     var severity = 'crowd_sensing';
  34.  
  35.  
  36.    var JsonMsg = request.body;
  37.  
  38.  
  39.   var JsonMsg2={
  40.       "user_id": "1",
  41.      "device_id":"sabri test",
  42.       "datetime":"10-22-2020 15:21:50",
  43.       "location":{
  44.           "lat":43.48,
  45.          "lng":-1.49    
  46.      },
  47.      "sensors":{
  48.          "temperature": "27"
  49.      }
  50.     }
  51.  
  52.  
  53.    // var JsonMsg=request.body;
  54.  
  55.     var msg = JSON.stringify(JsonMsg2);
  56. console.log(msg);
  57.     channel.publish(exchange, severity, Buffer.from(msg));
  58.     console.log(" [x] Sent %s: '%s'", severity, msg);
  59.  
  60.   });
  61.  
  62.   response.end("yes");
  63.  
  64. });
  65. });
  66.  
  67.  
  68. var server = app.listen(3000, function () {
  69.    var host = server.address().address
  70.    var port = server.address().port
  71.    console.log("Example app listening at http://%s:%s", host, port)
  72. })
RAW Paste Data