繁体   English   中英

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

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

如何从嵌套路由重定向到顶级路由?

在下面的代码中,路由/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
}););

作为快递文件,您可以传递一个相对路径来redirect

在您的情况下,您可以使用res.redirect('../profile'); 而不是res.redirect('/profile');

暂无
暂无

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

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