xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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



multipart/form-data


x-www-form-urlencoded


multipart/form-data vs x-www-form-urlencoded

https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

refs

Node.js - Express.js 从请求中获取二进制数据

https://www.coder.work/article/5080234

https://www.coder.work/article/5080234

https://juejin.cn/post/6844903710825381896

express 获取二进制数据

http://nodejs.dovov.com/express-20.html



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-04-08 21:00  xgqfrms  阅读(375)  评论(3编辑  收藏  举报