简体   繁体   English

如何设置类似 Instagram 的路线? 本地主机:3000/用户名

[英]How can I set a route like Instagram? localhost:3000/USERNAME

I want to set my routes like Instagram (instagram.com/username)我想设置我的路线,如 Instagram (instagram.com/username)

I tried this:我试过这个:

router.get('/:name', loggedin, function (req, res, next) {
res.render('profile', {"Request name": req.user});
});  

But if I visit my /logout route or any other route when I'm logged in it also render the profile Page instead of logging out但是,如果我在登录时访问我的 /logout 路由或任何其他路由,它也会呈现个人资料页面而不是注销

Extend your route by /logout to avoid using if statements and make use of POST requests.通过/logout扩展您的路线以避免使用 if 语句并使用 POST 请求。 You generally want most of your user-actions to be POST instead of GET requests.您通常希望大多数用户操作是 POST 而不是 GET 请求。

router.post('/:name/logout', loggedin, function (req, res, next) {
  // Logout user here
  // Return data or redirect
});  

Well just use a if statement then.那么只需使用if语句。

router.get('/:name', loggedin, function (req, res, next) {
   if(req.params.name == "logout"){
      //do something...
      return res.json("logged out");
   }
   res.render('profile', {"Request name": req.user});
});  

暂无
暂无

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

相关问题 我如何将 http post 请求从 localhost:5000 发送到 localhost:3000 - how can i send http post request from localhost:5000 to localhost:3000 当 react 应用程序未在 localhost:3000 上加载时,如何设置 webpack 和 babel - How do I set up webpack and babel when react app is not loading on localhost:3000 为什么我不能在localhost:3000上查看我的Express应用程序 - Why can I not view my express app on localhost:3000 为什么我不能在反应中使用 axios 从 localhost:3000 调用 localhost localhost:5000 - Why can't I call localhost localhost:5000 from localhost:3000 using axios in react 如何在反应中将 localhost:3000 更改为 custom.domain - How do I change localhost:3000 to custom.domain in react 为什么我得到“http://localhost:3000/”? 在反应中使用useNavigate时的路线? - Why am I getting 'http://localhost:3000/?' route when using useNavigate in react? 我的反应路线重定向到 localhost:3000 而不是它应该重定向到的页面 - My react route is redirecting to localhost:3000 and not the page it should redirect to 如何更改撰写 Instagram 等评论的时间? 使用 dayjs 在反应式中 - How can I change the time I write a comment like Instagram? in reactnative by using dayjs 如何重定向到instagram:// user?username = {username} - How to make redirect to instagram://user?username={username} 我怎样才能使用 Route 而不会出现这样的错误 - how can i use Route without getting any errors like this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM