如何将 req.body 从路由器文件传递到模块?

How to pass req.body from router file to module?

我正在尝试将我的代码分成模块,以便主要文件(应用程序和路由器)保持相对整洁。该网页接受来自表单的 POST 和 returns 用户使用用户输入生成的 PDF。

在 router.js 中,我想将 if 块的 {} 中的内容分成单独的文件(在第一个块中我将其注释掉)。但是,当我这样做时,单独的文件不会从中间件接收正文信息(我得到 ReferenceError: req is not defined)。

我如何将 if 块的 {} 导出到单独的文件中(这将变得非常冗长,并且会有更多的 if 块,这就是 example1.js 在文件夹 pdfGenerators 中的原因) router.js 保持清洁?

index.html

<!doctype html>
<form method="post" action="sendPDF">
  <input name="userInput" type="text">
  <button type="submit" name="submit" value="example1">Send Example 1</button>
</form>
<form method="post" action="sendPDF">
  <input name="userInput" type="text">
  <button type="submit" name="submit" value="example2">Send Example 2</button>
</form>

app.js

const express = require('express');
const app = express();
const path = require("path");    
app.use(express.urlencoded({ extended: false }));

//server started?
var port = process.env.PORT || 3000;
app.listen(port,() => {
    console.log('Started on PORT 3000');
  })
//For local testing only, using nginx normally
app.use(express.static(path.join(__dirname, "public"), {
    extensions: ['html']
}))

//sendPDF routing
require('./router')(app);

router.js

const PDFDocument = require('pdfkit');

module.exports = function(router) {
    router.post('/sendPDF', (req, res) => {

        if (req.body.submit =='example1') {
            require('./pdfGenerators/example1')();
            /*const doc = new PDFDocument();

            doc.text(req.body.userInput);

            // Pipe its output as http response
            res.statusCode = 200;
            res.setHeader('Content-type', 'application/pdf');
            res.setHeader('Content-disposition', 'attachment; filename=Example 1.pdf');
            doc.pipe(res);
            doc.end();*/
        }

        if (req.body.submit =='example2') {
            const doc = new PDFDocument();

            doc.text(req.body.userInput);

            // Pipe its output as http response
            res.statusCode = 200;
            res.setHeader('Content-type', 'application/pdf');
            res.setHeader('Content-disposition', 'attachment; filename=Example 2.pdf');
            doc.pipe(res);
            doc.end();
        }
    })
};

example1.js

const PDFDocument = require('pdfkit');
module.exports = function () {
const doc = new PDFDocument();

doc.text(req.body.userInput);

// Pipe its output as http response
res.statusCode = 200;
res.setHeader('Content-type', 'application/pdf');
res.setHeader('Content-disposition', 'attachment; filename=Example 1.pdf');
doc.pipe(res);
doc.end();

};

顺便问一下,我的思考方向是正确的还是我的架构逻辑搞砸了,这是使用 pdfkit 生成此类 pdf 的有效方法还是会导致多个同时用户出现问题?

您可以使用函数参数将 reqres 对象公开给 example1 模块。

在您的路由器中,

const PDFGenerator = require('./pdfGenerators/example1');
module.exports = function(router) {
    router.post('/sendPDF', (req, res) => {

        if (req.body.submit =='example1') {
            PDFGenerator(req, res);
        }
        // ...
}

然后在您的 PDFGenera 或 example1 模块中,您需要接受这些参数。

module.exports = function (req, res) {
    //...
}