【问题标题】:How to pass req.body from router file to module?如何将 req.body 从路由器文件传递到模块?
【发布时间】:2021-06-15 20:40:11
【问题描述】:

我正在尝试将我的代码分成模块,以便主文件(应用程序和路由器)保持相对整洁。该网页接受来自表单的 POST 并向用户返回由用户输入生成的 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);

路由器.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 的有效方法,还是会导致多个同时用户出现问题?

【问题讨论】:

  • example1.js 不应该处理 req 或 res 它的工作是加载或制作 pdf 文件,您可以导出自定义渲染或生成接受参数生成的函数,然后返回文档在控制器中做所有的响应工作.. 关注点分离,也不要重复多行代码来简单地更改文件名
  • 我同意将响应标头保留在路由器文件中(而不是重复代码)。但是,要生成 pdf example1.js 需要访问 req.body ,因此它无法避免该特定方面,还是我想不出另一种方法?

标签: node.js express body-parser node-pdfkit


【解决方案1】:

您可以使用函数参数将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) {
    //...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2016-06-19
    • 2021-07-29
    • 2018-08-10
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多