简体   繁体   English

Node.js表示登录cookie和URL

[英]Node.js express login cookie and URL

I am quite new to Node.js and I have two problems I cannot seem to be able to solve nor find any solutions on the internet. 我对Node.js还是很陌生,但有两个问题似乎无法解决,也找不到任何解决方案。

  1. If I want to create a login function (I have everything setup already except the cookies part) I have to somehow get the servers response, set a cookie and send it (express). 如果我想创建一个登录功能(除了cookie部分,我已经进行了所有设置),我必须以某种方式获取服务器响应,设置一个cookie并将其发送(表达)。

Now I'm wondering how should I get into this to use the variable in the login function? 现在我想知道如何在登录功能中使用该变量?

The login function accepts callback, request, response as parameters. 登录函数接受callback, request, response作为参数。 I wanted to return a callback whether the cookie is set after setting it. 我想在设置cookie后返回一个是否设置cookie的回调。 But the problem occurs when I want to set the cookie. 但是,当我要设置cookie时,会出现问题。 I have to use response.send() which results in an infinite loop. 我必须使用response.send()导致无限循环。

  1. Is it possible to visit for example localhost/mywebsite and still get the server to work instead of localhost:8080 ? 是否可以访问例如localhost / mywebsite并仍使服务器正常运行而不是localhost:8080 (8080 is port I'm listening to, as an example) (例如,8080是我正在监听的端口)

This is a wonderful guide on setting up authentication in Express using a tried-and-true authentication library called Passport . 是有关使用称为Passport的可靠验证库在Express中设置验证的绝妙指南。 And here is a second guide using an enterprise-grade (there's a free version) library from Auth0 and it has a lot more features regarding identity. 是使用Auth0的企业级(有免费版本)库的第二本指南,它具有许多有关身份的功能。

Update 更新资料
So once the user logs in, you could send a cookie like this: res.cookie(cookieObject).send(responseObject) - more on this here . 因此,一旦用户登录,您可以发送如下cookie: res.cookie(cookieObject).send(responseObject) -有关此内容的更多信息 It would then be the browser/client-side framework's responsibility to return the cookie with every request. 然后,浏览器/客户端框架的责任是随每个请求返回cookie。 You could use the cookieparser package from NPM to help parse cookies from the req object. 您可以使用NPM中的cookieparser包来帮助解析req对象中的cookie。 Let us know if this helps. 让我们知道是否有帮助。

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

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