简体   繁体   English

如何使用节点快递从嵌套路由重定向到顶级路由?

[英]How to redirect to a top route from a nested route with node express?

How can I redirect to a top route from a nested route?如何从嵌套路由重定向到顶级路由?

On the code below, the callback for the route /toproute/nested should redirect to /profile instead of /toproute/profile .在下面的代码中,路由/toproute/nested的回调应该重定向到/profile而不是/toproute/profile

// app.js
const express = require('express');
const app = express();
const topRoutes = require('./top-routes');

app.use('/toproute', topRoutes);

app.use('/about', (req, res) => {
    res.send('About');
});

app.listen(4200, () => {});
// top-routes.js - /toproute

const router = require('express').Router();

router.use('/nested', (req, res) => {
  res.redirect('/profile'); // Should redirect to /profile instead of /toproute/profile
}););

As express document , you can pass a relative path to redirect .作为快递文件,您可以传递一个相对路径来redirect

In your case, you can use res.redirect('../profile');在您的情况下,您可以使用res.redirect('../profile'); instead of res.redirect('/profile');而不是res.redirect('/profile');

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

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