Forward request to alternate request handler instead of redirect

前端 未结 7 2133
借酒劲吻你
借酒劲吻你 2020-12-07 22:24

I\'m using Node.js with express and already know the existence of response.redirect().

However, I\'m looking for more of a forward() funct

相关标签:
7条回答
  • 2020-12-07 23:03

    Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call

    router.get("/a/path", function(req, res){
        req.url = "/another/path";
        router.handle(req, res);
    }
    
    0 讨论(0)
  • 2020-12-07 23:04

    You just need to invoke the corresponding route handler function.

    Option 1: route multiple paths to the same handler function

    function getDogs(req, res, next) {
      //...
    }}
    app.get('/dogs', getDogs);
    app.get('/canines', getDogs);
    

    Option 2: Invoke a separate handler function manually/conditionally

    app.get('/canines', function (req, res, next) {
       if (something) {
          //process one way
       } else {
          //do a manual "forward"
          getDogs(req, res, next);
       }
    });
    

    Option 3: call next('route')

    If you carefully order your router patterns, you can call next('route'), which may achieve what you want. It basically says to express 'keep moving on down the router pattern list', instead of a call to next(), which says to express 'move down the middleware list (past the router)`.

    0 讨论(0)
  • 2020-12-07 23:07

    You can use run-middleware module exactly for that. Just run the handler you want by using the URL & method & data.

    https://www.npmjs.com/package/run-middleware

    For example:

    app.runMiddleware('/get-user/20',function(code,body,headers){
        res.status(code).send(body)
    })
    
    0 讨论(0)
  • 2020-12-07 23:08

    Express 4+ with nested routers

    Instead of having to use the outside of route/function app, you can use req.app.handle

    "use strict";
    
    const express = require("express");
    const app = express();
    
    //
    //  Nested Router 1
    //
    const routerOne = express.Router();
    
    //  /one/base
    routerOne.get("/base", function (req, res, next) {
      res.send("/one/base");
    });
    
    //  This routes to same router (uses same req.baseUrl)
    //  /one/redirect-within-router -> /one/base
    routerOne.get("/redirect-within-router", function (req, res, next) {
      req.url = "/base";
      next();
    });
    
    //  This routes to same router (uses same req.baseUrl)
    //  /one/redirect-not-found -> /one/two/base (404: Not Found)
    routerOne.get("/redirect-not-found", function (req, res, next) {
      req.url = "/two/base";
      next();
    });
    
    //  Using the full URL
    //  /one/redirect-within-app -> /two/base
    routerOne.get("/redirect-within-app", function (req, res, next) {
      req.url = "/two/base";
    
      // same as req.url = "/one/base";
      //req.url = req.baseUrl + "/base";
    
      req.app.handle(req, res);
    });
    
    //  Using the full URL
    //  /one/redirect-app-base -> /base
    routerOne.get("/redirect-app-base", function (req, res, next) {
      req.url = "/base";
      req.app.handle(req, res);
    });
    
    
    
    //
    //  Nested Router 2
    //
    const routerTwo = express.Router();
    
    //  /two/base
    routerTwo.get("/base", function (req, res, next) {
      res.send("/two/base");
    });
    
    //  /base
    app.get("/base", function (req, res, next) {
      res.send("/base");
    });
    
    
    //
    //  Mount Routers
    //
    app.use("/one/", routerOne);
    app.use("/two/", routerTwo);
    
    
    //  404: Not found
    app.all("*", function (req, res, next) {
      res.status(404).send("404: Not Found");
    });
    
    0 讨论(0)
  • 2020-12-07 23:09
    app.get('/menzi', function (req, res, next) {
            console.log('menzi2');
            req.url = '/menzi/html/menzi.html';
            // res.redirect('/menzi/html/menzi.html');
            next();
    });
    

    This is my code:when user enter "/menzi",the server will give the page /menzi/html/menzi.html to user, but the url in the browser will not change;

    0 讨论(0)
  • 2020-12-07 23:16

    For Express 4+

    Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call

    app.get("/a/path", function(req, res){
        req.url = "/another/path";
        app.handle(req, res);
    }
    

    Or for HTML5 mode of React/Angular

    const dir = process.env.DIR || './build';
    
    // Configure http server
    let app = express();
    app.use('/', express.static(dir));
    
    // This route sends a 404 when looking for a missing file (ie a URL with a dot in it)
    app.all('/*\.*', function (req, res) {
        res.status(404).send('404 Not found');
    });
    
    // This route deals enables HTML5Mode by forwarding "missing" links to the index.html
    app.all('/**', function (req, res) {
        req.url = 'index.html';
        app.handle(req, res);
    });
    
    0 讨论(0)
提交回复
热议问题