node.js - 如何将 req.body 从路由器文件传递到模块?
问题描述
我正在尝试将我的代码分成模块,以便主文件(应用程序和路由器)保持相对整洁。该网页接受来自表单的 POST 并向用户返回由用户输入生成的 PDF。
在 router.js 中,我想将if
块的 {} 中的内容分成单独的文件(在第一个块中我将其注释掉)。但是,当我这样做时,单独的文件不会从中间件接收正文信息(我得到ReferenceError: req is not defined
)。
如何将 if 块的 {}(这将变得非常冗长,并且还会有更多 if 块,这就是 example1.js 位于 pdfGenerators 文件夹中的原因)到单独的文件中,以便 router.js 保持干净?
索引.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>
应用程序.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();
}
})
};
例子1.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 的有效方法,还是会导致多个同时用户出现问题?
解决方案
您可以使用函数参数作为向模块公开req
和res
对象的一种方式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) {
//...
}
推荐阅读
- python - 在 Matplotlib 中使用子图时如何控制颜色条位置
- css - 单击导航菜单时如何激活状态?
- mysql - 在 where 子句中使用参数的超集 sql 查询编辑器
- python - 当我绘制超过 2 个值时,为什么 Timer 会破坏我的数据?
- sql - 在 sql server 2016 中创建内存表时的桶计数帮助
- azure - 按每个事件组的时间戳显示第一个条目
- python - 将字符串 numpy.ndarray 转换为浮点数 numpy.ndarray
- django - 更改数据库后重新启动 Django 服务器
- c# - 如何实现 Android ISO 并将其加载到 C# WinForm 应用程序中?
- marklogic - 路径范围索引方面搜索在 Marklogic 9 中不起作用