天天看點

Node.js & Express.js Server get binary data All In One

Node.js & Express.js Server get binary data All In One

Node.js & Express.js Server get binary data All In One

node.js 接收二進制資料
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const hostname = 'localhost';
// const hostname = '127.0.0.1';
// const hostname = 'http://10.1.159.45';
// const port = 3000;
const port = 9000;
const app = express();
// Starting with release 4.16.0, a new express.json() middleware is available.
app.use(express.json());
app.use(express.urlencoded({
  extended: true,
}));

app.use(function (req, res, next) {
  // JSON parse
  // console.log('req.body', req.body);
  // CORS bug
  res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  // res.header("Content-Security-Policy", "connect-src *");
  // res.header("Content-Security-Policy", "connect-src '*'");
  // res.header("Content-Security-Policy", "connect-src 'localhost'");
  // res.header("Content-Security-Policy", "connect-src localhost");
  // Content-Security-Policy: connect-src <source>;
  // Content-Security-Policy: connect-src <source> <source>;
  // res.header('Content-Type', 'application/json');
  // res.setHeader('Content-Type', 'application/json');
  next();
});
// https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=555a887d-c247-4808-a850-22440c5b25a0


app.get('/api/get', (req, res) => {
  // console.log('get req', req);
  console.log('req.params', req.params);
  console.log('req.query', req.query);
  // console.log('req.query', req.query, JSON.stringify(JSON.parse(req.query), null, 4));
  const obj = JSON.parse(req.query.q);
  console.log('obj =', JSON.stringify(obj, null, 4));
  res.setHeader('Content-Type', 'text/html');
  res.send('get api');
});

app.post('/api/post', (req, res) => {
  // console.log('❌ post req', req);
  console.log('✅ post req.body', req.body);
  // res.setHeader('Content-Type', 'application/json');
  // res.sendStatus(200);
  let msg=[];
  req.on('data',(chunk)=>{
    console.log('chunk', chunk);
    if(chunk){
      msg.push(chunk);
    }
  })
  // 接收完畢
  req.on('end',()=>{
    // 對buffer數組陣列清單進行buffer合并傳回一個Buffer
    console.log('msg', msg);
    let buf = Buffer.concat(msg);
    console.log(buf);
    //提取 Buffer 正确
  })
  res.send({res: 'post api'});
});

// app.post('/api/beacon', (req, res) => {
//   // console.log('❌ beacon req', req);
//   console.log('✅ beacon post req.body', req.body);
//   // res.setHeader('Content-Type', 'application/json');
//   // res.sendStatus(200);
//   res.send({res: 'beacon api'});
// });

app.listen(port, () => {
    console.log(`
        Server is running at http://${hostname}:${port}/
        Server hostname ${hostname} is listening on port ${port}!
    `);
});


/*

http://127.0.0.1:9000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D
http://localhost:9000/api/get?q=%7B%22username%22:%22xgqfrms%22%7D

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



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

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


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



*/


      

multer

​​http://expressjs.com/en/resources/middleware/multer.html​​

$ npm install --save multer

      

Beacon API & multipart/form-data

Node.js &amp; Express.js Server get binary data All In One

multipart/form-data

x-www-form-urlencoded

​multipart/form-data​

​ vs ​

​x-www-form-urlencoded​

Node.js &amp; Express.js Server get binary data All In One