Skip to content

Instantly share code, notes, and snippets.

@xgqfrms
Created April 6, 2021 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgqfrms/8570bf512c2c111069933bc359e30f0e to your computer and use it in GitHub Desktop.
Save xgqfrms/8570bf512c2c111069933bc359e30f0e to your computer and use it in GitHub Desktop.
express.js & FormData

express.js & FormData

@xgqfrms
Copy link
Author

xgqfrms commented Apr 6, 2021

image

@xgqfrms
Copy link
Author

xgqfrms commented Apr 6, 2021

const obj = {
    "name": "工作表-数据概览-整体数据",
    "url": "http://10.1.159.45:8088/table-2/data/global",
    "path": "/table-2/data/global",
    "host": "10.1.159.45:8088"
}

const logVisit = (obj = {}, text = '') => {
  const url = 'http://localhost:3000/api/post';
  // Test that we have support
  if (!navigator.sendBeacon) {
      // XHR fallback
      return true;
  } else {
      // URL to send the data to, e.g.
      // let url = `/api/log`;
      // Data to send
      let data = new FormData();
      // data.append(`start`, startTime);
      // data.append(`end`, performance.now());
      // data.append(`url`, document.URL);
      const keys = Object.keys(obj);
      for (const key of keys) {
          data.append(key, obj[key]);
      }
      if(text) {
          data.append('desc', text);
      }
      // Let`s go!
      console.log(`Beacon API 🔥 DAP 数据上报 = ${JSON.stringify(obj, null, 4)}`);
      navigator.sendBeacon(url, data);
  }
};

logVisit(obj, 'shit api');
fetch(`http://localhost:3000/api/post`, {
    body: JSON.stringify({key: "value"}),
    cache: "no-cache",
    headers: {
        "Content-Type": "application/json",
    },
    method: "POST",
    mode: "cors",
})
.then(res => {
   console.log(`res =`, res)
   return res.json()
})
// .then(res => res.json())
.then(json => console.log(`json =`, json))
.catch(err => console.error(`error =`, err));

http://localhost:3000/api/get?q={%22username%22:%22xgqfrms%22}

@xgqfrms
Copy link
Author

xgqfrms commented Apr 6, 2021

@xgqfrms
Copy link
Author

xgqfrms commented Apr 6, 2021

express formdata request

https://flaviocopes.com/express-forms/

https://medium.com/cubemail88/node-js-express-js-body-parser-%E8%99%95%E7%90%86multipart-form-data%E7%9A%84%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88-d89d2699b9f

const express = require('express')
var bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/send', (req, res) => {
    let formData = req.body;
    console.log('form data', formData);
    res.sendStatus(200);
  });

app.listen(3000, () => console.log('Example app listening on port 3000!'))

https://stackoverflow.com/questions/24800511/express-js-form-data

https://www.tutorialspoint.com/expressjs/expressjs_form_data.htm

@xgqfrms
Copy link
Author

xgqfrms commented Apr 6, 2021

Access to fetch at 'http://localhost:3000/api/post' from origin 'http://10.1.159.45:8088' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

@xgqfrms
Copy link
Author

xgqfrms commented Aug 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment