【问题标题】:Saving into localStorage with nodejs使用 nodejs 保存到 localStorage
【发布时间】:2016-09-08 00:45:30
【问题描述】:

用户登录(已授权)后,我需要通过nodejs将jwt-token保存在本地存储中。

在我检查控制器中的用户/密码是否正确后 - 我将生成的令牌保存在本地存储中。就目前而言,我无法引用窗口,因为它不存在。

ReferenceError: window is not defined 

这就是我目前正在尝试的方式。

...
      payload = {
        sub: user.email,
        role: user.role
      };

      token = jwt.sign(payload, jwtSecretKey, {expiresIn: '60m'});

      window.localStorage.setItem('token', token);

      res.status(200).render('index' , {
        user: user,
        token: token
      });
...

【问题讨论】:

  • 你知道客户端和服务器有什么区别吗?
  • 那么...你为什么要在服务器上做客户端的事情?
  • 我想知道这是否可能。我将引用其中一个链接线程中的人的话“有很多模块将 DOM 带到服务器端,将 webkit 带到服务器端,所以我很好奇是否有人将 localStorage 带到了服务器端。 "
  • 您可以在客户端使用 jquery 或 angularjs 访问本地存储。所以,把你的代码放在那里:)
  • 嘿! @Paran0a。你能检查你的问题的答案。我的可能是正确的:))

标签: javascript node.js express local-storage


【解决方案1】:

您无法在 Node.js 上保存到 localStorage,但您可以在浏览器上执行此操作,可能在从服务器发送之后,从 Node.js 上的服务器发送。

您应该将令牌从服务器(在 Node.js 上运行)发送到具有 window 对象、具有 localStorage 和相关的 getItem 和 setItem 方法的客户端(浏览器),您可以来自客户端(浏览器)的 JavaScript 代码的引用。 Node.js 没有任何 window 可供参考。所以,通过在 Node.js 代码中引用它是一个 ReferenceError 提示程序代码正在引用一个未定义的东西,undefined,你遇到了。

只需将其放入 cookie 并发送,或通过 json 响应发送。然后在客户端浏览器上保存到window.localStorage。

以下是后一种方式的示例代码;通过响应发送:

// SERVER-SIDE Code
// say `app` is your app server on node.js
// this is a url handler on your server-side code
// you just have sent your user credentials from a browser
// typically via a form in the body of your http request
app.post('/auth', function (req, res) {
  // you may have here some jwt token creation things
  // ...
  // and send it as your response to the client (probably a web browser) ..
  // with your prefrred name as the key, say 'my_token', ..
  // where you will save it to localStorage (ie. window.localStorage)
  res.json({my_token: 'asdfgh-anything-jw-token-qwerty'})
})

  // CLIENT-SIDE Code (may be a web browser)
  // You have just sent a request to the server..
  // ..with user credentials for authentication in the request body
  // in the request body may be a window.FormData object or a json etc.
  http.post('auth', userCredentials)
    // probably the request mechanism you make http..
    // ..requests asynchronously, maybe using a library,..
    // ..will return a Promise, and you will have a similar code below.
    .then(response => response.json())
    .then(responseJson => {
      // set localStorage with your preferred name,..
      // ..say 'my_token', and the value sent by server
      window.localStorage.setItem('my_token', responseJson.my_token)
      // you may also want to redirect after you have saved localStorage:
      // window.location.assign("http://www.example.org")
      // you may even want to return responseJson or something else or assign it to some variable
      // return responseJson;
  })

【讨论】:

  • 嗨,你能举个例子,如何通过json响应发送它
  • 这里用这个fiddle as an example,你可以随时回问
  • 嗨,兄弟,在感谢您的帮助之前我想通了,但是我正在努力重定向页面,您能帮我吗?
  • @kannan 如果您将在没有 cookie 的请求中将令牌发送到客户端,请在客户端进行重定向,并在上面的代码行之后设置 window.localStorage。否则,您将丢失您的令牌,即从服务器端发送的数据,在您重定向页面时将发生的整页重新加载之间。要在此设置操作后重定向您的网页,请使用window.location,只需将其分配给您将重定向到的地址,如window.location.assign('http://example.com/')
  • 明白兄弟我发送没有cookie的令牌jsfiddle.net/vqbfxrnj这是nodejs登录代码。这是我的前端登录代码jsfiddle.net/97boc34q
【解决方案2】:

如果您的意思是 html 5 localStorage,则没有这样的东西,因为 node.js 是一种服务器端技术。 Html 5 localStorage 是支持的客户端功能

参考How to access localStorage in node.js?

【讨论】:

  • 是的,我说的是存储。我见过很少有人推荐将生成的令牌保存到 cookie 或本地存储中的教程。所以如果我想从节点保存它,我应该使用 cookie?
  • 是的,同样可以使用 cookie
  • 您可以使用 cookie 或 json 响应。
【解决方案3】:

在客户端调用 /login 时使用 xmlhttpresponse 对象,然后为“加载”添加一个事件侦听器。这将为客户端提供您添加令牌的 responseObject。然后在事件监听器中放入你的 localStorage 代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 2016-08-27
    • 2021-12-26
    • 2014-01-27
    • 2013-11-25
    • 2013-02-24
    • 2011-12-10
    • 2017-11-03
    相关资源
    最近更新 更多