Node JS:如何在一条路线中打开另一条路线

Node JS: How to open one route within another

我是网络前端领域的新手。我有路线:/product/shop。对于每条路线,我都创建了控制器。问题是如何从 /product 页面打开 /shop 路由页面。当我调用 res.render('product'); 时,它路由 http://localhost:3000/shop/products 而不是 http://localhost:3000/products

我该如何解决这个问题?

您被重定向到 http://localhost:3000/shop/products 因为执行请求之前的当前路径是:http://localhost:3000/shop 要直接继续/products,您可以执行以下操作

  • 您可以使用 .. 调用文件系统中的父级(使用相对路径)
res.render('../products')
  • 或者简单地使用绝对路径
res.render('/products')

使用

redirect

在您的路线中 Express 重定向

示例:

app.get('/shop', function(req, res) {
 res.redirect('/products');
});