简体   繁体   English

使功能成为路由中的快速中间件

[英]Making a function an express middleware in a route

So I have a route and I have some ugly code in the route that I'd like to make a middleware. 因此,我有一条路线,并且我要制造中间件的路线中有一些难看的代码。 The only problem is that Express's documentation isn't clear and my code just 404s. 唯一的问题是Express的文档不清楚,而我的代码只是404s。

How can I accomplish this? 我该怎么做?

Route: 路线:

router.get('/product/:slug', function(req, res) {
//route params
  var slug = req.params.slug;
  var productResp; //scope up api response to pass to render()
  console.log(slug);
//api call
  Prismic.api("https://prismic.io/api").then(function(api) {
    return api.getByUID('product' , slug);
  }).then(function(response) {

   app.use(markedHtml)
  })
  .catch(function(error) {
    res.render('404');
  })
});

Function: 功能:

  var markedHtml = function(req, res, next) {
      var md_col_1 = response.data["product.markdown-col-one"].value[0].text;
      var md_col_1_1 = response.data["product.markdown-col-one"].value[1].text;
      var md_col_2 = response.data["product.markdown-col-two"].value[0].text;
      var md_col_2_1 = response.data["product.markdown-col-two"].value[1].text;
      var md_col_3 = response.data["product.markdown-col-three"].value[0].text;
      var md_col_3_1 = response.data["product.markdown-col-three"].value[1].text;


      var html_col_1 = marked(md_col_1);
      var html_col_1_1 = marked(md_col_1_1);
      var html_col_2 = marked(md_col_2);
      var html_col_2_1 = marked(md_col_2_1);
      var html_col_3 = marked(md_col_3);
      var html_col_3_1 = marked(md_col_3_1);



    res.render('product-template', {
      product: response,
      md_one: html_col_1,
      md_one_1: html_col_1_1,
      md_two: html_col_2,
      md_two_1: html_col_2_1,
      md_three: html_col_3,
      md_three_1: html_col_3_1,
    })

      next();
    }

In your root app.js , you should see all of the middleware your app is using. 在您的根app.js ,您应该看到您的应用程序正在使用的所有中间件。 These middleware are matched sequentially. 这些中间件顺序匹配。 If they do not include the route argument, they will naturally be applied to all requests. 如果它们不包括route参数,则它们自然会应用于所有请求。 Your routes are among them, and they look something like this: app.use('/', routes); 您的路线就在其中,它们看起来像这样: app.use('/', routes); .

Underneath your routes in app.js , declare a new one: app.js的路线下,声明一个新路线:

const markedHtml = require('./middleware/markedHtml');
app.use('/product/:slug', markedHtml);

In ./middleware/marketHtml.js , your method will appear like so, without the next call: ./middleware/marketHtml.js ,您的方法将像这样显示,而无需next调用:

const markedHtml = function(req, res, next) {
  // Do stuff with req.apiResponse here.
  ...

  res.render('product-template', {
    ...
  });
}

module.exports = markedHtml;

Your original route will look like this: 您的原始路线将如下所示:

router.get('/product/:slug', function(req, res, next) {
  //route params
  var slug = req.params.slug;
  var productResp; //scope up api response to pass to render()
  console.log(slug);

  //api call
  Prismic.api("https://prismic.io/api").then(function(api) {
    return api.getByUID('product' , slug);
  }).then(function(response) {
    req.apiResponse = response;
    next();
  })
  .catch(function(error) {
    res.render('404');
  });
});

Essentially, your original route will receive the client request, do the API calls etc, then (pun!) run into the next(); 本质上,您的原始路由将接收客户端请求,执行API调用等,然后(pun!)运行到next(); invocation. 调用。 That cues express to execute the next applicable middleware, which happens to be markedHtml , from where the view render method will be invoked. 这些提示表示要执行下一个适用的中间件,该中间件恰好是markedHtml ,将从中调用视图渲染方法。

Let me know if anything was unclear or requires additional explanation. 让我知道是否有不清楚的地方或需要其他说明。

Additional documentation can be found here: http://expressjs.com/en/4x/api.html#app.use 可以在以下位置找到其他文档: http : //expressjs.com/en/4x/api.html#app.use

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

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