简体   繁体   English

Node.js Express –如何获取原始数据?

[英]Node.js Express – How to Get Raw Data?

I have an Express server that manages a login form page: 我有一个管理登录表单页面的Express服务器:

const app = express();

// section A
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded());

app.get('/login', userController.getLogin);
app.post('/login', userController.postLogin);

It runs correctly. 它可以正常运行。

Then I have another controller that reads RAW DATA: 然后,我还有另一个读取原始数据的控制器:

// section B
const concat = require('concat-stream');
app.use(function(req, res, next) {
  req.pipe(concat(function(data: any) {
    req.body = data;
    next();
  }));
});

app.post('*', otherController.post);

export let post = (req: any, res: Response) => {
  console.log(req.body); //i see RAW DATA
  res.end('n');
}

It also works well. 它也很好用。

But if I join the two sections, the second section stops working. 但是,如果我加入这两个部分,则第二部分将停止工作。

How can I say to use the req.pipe only for the section B? 我怎么说只能在B部分中使用req.pipe

This middleware (like virtually every other middleware out there), does not have a way to exclude it from certain requests, since the method in which users would desire to exclude can be anything--url, header, query string, loaded user, and more. 该中间件(几乎像其他所有中间件一样)没有一种将其从某些请求中排除的方法,因为用户希望排除的方法可以是任何东西-网址,标头,查询字符串,加载的用户和更多。 Instead middleware places the logic of pathing on you: either (a) explicitly include it (which means placing on the routes, as recommended in our documentation) or (b) wrap the middleware with your own exclusion logic: 相反,中间件将路径逻辑放在您身上:(a)显式地包括它(这意味着按照我们的文档中的建议放置在路由上)或(b)用您自己的排除逻辑包装中间件:

const parseExtend = bodyParser.urlencoded({ extended: true });
app.use((req, res, next) => shouldParseRequest(req) ? parseExtend(req, res, next) : next());

/* implement shouldParseRequest (req) to return false for whatever you want to not parse json for */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 express.js 在 Node.js 中获取原始 HTTP 标头字符串 - How to get raw HTTP header string in Node.js with express.js 如何从 Express (Node.js) 中的表单获取数据 - How to get data passed from a form in Express (Node.js) Want to call an api to get the data from mongodb in json format but getting in raw format, using javascript, node.js, express, mongodb - Want to call an api to get the data from mongodb in json format but getting in raw format, using javascript, node.js, express, mongodb 在Node.js中处理“原始数据”并在Node Express端点中传递数据 - Handling “raw data” in Node.js and passing on the data in a Node Express endpoint 如何通过击中URL然后使用node.js和express.js将其呈现为html来获取数据 - How to get data by hitting an url and then rendering it to html using node.js and express.js 通过Express从Node.js获取数据 - Get data from Node.js via Express Node.js 和 express.js 如何读取“get”端点? - Node.js and express.js how to read 'get' endpoint? 使用node.js express从数据库获取和处理数据 - GET and handle data from DB with node.js express node.js,express,如何在post请求中从body form-data中获取数据 - node.js, express, how to get data from body form-data in post request 如何在Node.js中获取POSTed(jquery)数组数据(使用express) - How to get POSTed (jquery) array data in Node.js (using express)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM