节点JS:如何打开另一个路线

我是networking前端世界的新手。 我有路线: /product/shop 。 并为每条路线创build了控制器。 问题是我如何打开/shop /product页面的路线页面。 当我打电话给res.render('product'); 它路由http://localhost:3000/shop/products而不是http://localhost:3000/products

我如何解决这个问题?

您被redirect到http://localhost:3000/shop/products因为执行请求之前的当前path是: http://localhost:3000/shop要直接进入/products ,您可以执行以下操作

  • 您可以使用..来调用文件系统中的父项(使用相对path)
 res.render('../products') 
  • 或者只是使用绝对path
 res.render('/products') 

使用

redirect

在您的路线快递redirect

例:

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