简体   繁体   English

通过从Node.js和Express.js中的前端请求数据来创建动态链接

[英]Creating dynamic link by requesting data from front-end in Node.js and Express.js

This is kind of hard for me to explain but I am trying to shorten my code by creating one link instead of 50 for downloading a form. 这对我来说很难解释,但是我试图通过创建一个链接而不是50个链接来下载表单来缩短代码。 I will try to explain this a bit better using my code. 我将尝试使用我的代码更好地解释这一点。

I have 5 get requests that do exactly the same thing but download a different file. 我有5个get请求,它们执行的功能完全相同,但下载的文件不同。

router.get('/form1', function (req, res) {
    var file = __dirname + '/../public/forms/form1.pdf';
    res.download(file);
});

router.get('/form2', function (req, res) {
    var file = __dirname + '/../public/forms/form2.pdf';
    res.download(file); 
});
etc...

and my front-end link are; 和我的前端链接是;

<a href="/downloads/form1">FORM 1</a>
<a href="/downloads/form2">FORM 2</a>
etc...

Is there anything I can do to make this a more dynamic? 我能做些什么使它更具动态性吗? The only way I can think of is something like this; 我唯一能想到的就是这样。

router.get('/:formName', function (req, res) {
    // some how do a "req.params.formName" 
    var file = __dirname + '/../public/forms/' + req.params.formName + '.pdf';
    res.download(file); // Set disposition and send it.
});

But I don't know how I will get the formName or if thats even possible. 但是我不知道如何获取formName,或者那是否有可能。

This response assumes that your route lives in index.js . 该响应假定您的路线位于index.js

router.get('/form/:formName', (req, res, next) => {
  res.download(`${__dirname}/../public/forms/${req.params.formName}.pdf`);
});

<a href="/form/form2">FORM 2</a>

Be careful about your path. 小心自己的路。 I don't know whether you can start at a directory, go up a level, then down again. 我不知道您是否可以从目录开始,再上一个级别,然后再下一个。

Here are some more options to clarify: 这里有一些其他选项可以澄清:

  • Option 1: If you have a folder on the server with a fairly manageable directory structure, simply use express.static to map the physical folder to a virtual one with automatic download: 选项1:如果服务器上的文件夹具有可管理的目录结构,则只需使用express.static将物理文件夹映射到虚拟文件夹并自动下载:

     app.use('/download', express.static(path.join(__dirname, '/public/forms/'))) 

    This will result in any link from the front-end with href='/download/something.pdf' working as long as that file is on the server in the path you mapped (ie in /public/forms). 只要该文件在服务器上您所映射的路径(即/ public / forms)中,这将导致来自前端的任何带有href ='/ download / something.pdf'的链接正常工作。

  • Option 2 (which David E above answered in essence): In your original code, if you wanted to generate a path handler for a link that looks like /download/form1, /download/form2, it's a very minor modification: 选项2(上面的David E实质上回答了该问题):在您的原始代码中,如果您想为看起来像/ download / form1,/ download / form2的链接生成路径处理程序,这是一个非常小的修改:

     router.get('/download/:formNumber', function (req, res) { var file = __dirname + '/public/forms/' + req.params.formNumber + '.pdf'; res.download(file); }); 
  • Option 3: You want to authenticate access to the files and potentially support multiple, complex URL schemes to a single handler that can lookup the appropriate physical path and send the file: 选项3:您想要验证对文件的访问,并可能支持对单个处理程序的多种复杂URL方案,这些处理程序可以查找适当的物理路径并发送文件:

     router.get('/download/:path[forms|images|files]/:formNumber1', fileRequestHandler); router.get('/public/downloadFolder2/:formNumber2', fileRequestHandler); router.get('/public/downloadFolder3/:formNumber3', fileRequestHandler); function fileRequestHandler(req, res) { // Check authentication here - example below from Passport if(!req.isAuthenticated()) return res.status(401).send({err: 'Unauthorized'}); // Check which form number is supplied and map to appropriate physical file if(req.params.formNumber1) { // in this example, req.params.path can be one of three allowed sample values - forms or images or files var file = __dirname + '/public/' + req.params.path + '/' + req.params.formNumber + '.pdf'; res.download(file); } else if(req.params.formNumber2) { // etc. } } 

Note: Ezra Chang's point about path validity is important. 注意: Ezra Chang关于路径有效性的观点很重要。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM