Advertisement
Sinken

Untitled

May 21st, 2022
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sender
  2. function save() {
  3.     let csvContent = "";
  4.     var transposed = map[0].map((_, colIndex) => map.map(row => row[colIndex]));
  5.     transposed.forEach(function(rowArray) {
  6.         let row = rowArray.join("\t");
  7.         csvContent += row + "\n";
  8.     });
  9.  
  10.     console.log("Sending", JSON.stringify(transposed))
  11.     fetch("http://localhost:8000/", {
  12.         method: "POST",
  13.         body: JSON.stringify(csvContent),
  14.         headers: {
  15.             'Content-type': 'application/json' // The type of data you're sending
  16.         }
  17.     })
  18. }
  19.  
  20.  
  21. //Mottar, egentlig i en annen fil
  22. const http = require('http');
  23. const fs = require("fs");
  24.  
  25. var express = require('express')
  26. var app = express()
  27. var bodyparser = require('body-parser')
  28.  
  29. var allowCrossDomain = function(req, res, next) {
  30.     res.header('Access-Control-Allow-Origin', '*');
  31.     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  32.     res.header('Access-Control-Allow-Headers', 'Content-Type');
  33.    
  34.     next();
  35. }
  36.  
  37. app.use(allowCrossDomain)
  38. app.use(express.json());
  39.  
  40. app.post('/', function(req, res) {
  41.  
  42.     var csv = req.body;
  43.     console.log((req.body))
  44.     console.log(typeof csv)
  45.     fs.writeFile("maps/req.csv", csv, function (err) {
  46.         if (err) return console.log(err);
  47.       })
  48. });
  49.  
  50. const server = http.createServer(app).listen(8000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement