简体   繁体   中英

Node.js Express - Module vs Middleware

I got an express rest api and want to generate a pdf and return a link to the file. I already figured out how to gernerate the pdf. Now I want to have a route that just returns the pdf link and other routes that do their own stuff but also generate a pdf. Should I use a normal node module for the pdf generator or make a middleware that does it?

Preferably normal node module because you're not going to generate various pdf templates you can have a common util to generate but for templating purpose keep it seperate.

If you know how to genarate pdf so

Just use res.download

It's common express method

res.download('/report-12345.pdf', 'report.pdf', function (err) {
  if (err) {
    // Handle error, but keep in mind the response may be partially-sent
    // so check res.headersSent
  } else {
    // decrement a download credit, etc.
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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