nodeJS localstorage获取值

时间:2015-10-19 07:54:27

标签: javascript node.js local-storage

浏览器的本地存储中有一个名为userToken的密钥,我试图获取它,以便使用我的私钥进行解码。

我使用的是名为node-localstorage的软件包,可在此处找到 https://github.com/lmaccherone/node-localstorage

但它会返回null

这可能是什么问题?

app.get('/getitnow', function(req,res) {
    var LocalStorage = require('node-localstorage').LocalStorage;
    var localStorage = new LocalStorage('./scratch');
    console.log(localStorage.getItem('userToken'));

});

3 个答案:

答案 0 :(得分:0)

localstorage取决于浏览器,NodeJ是服务器端。

尝试使用会话:

var express = require('express');
var app = express();

app.use(express.cookieParser());
app.use(express.session({secret: '12345'}));

答案 1 :(得分:0)

那不会从客户那里获得价值。 node-localstorage是node的localstorage。这就是为什么它返回null,因为您还没有在目录临时设置任何值。

答案 2 :(得分:0)

app.get('/getitnow', (req,res)=> {
    var LocalStorage = require('node-localstorage').LocalStorage;
    var localStorage = new LocalStorage('./scratch');
    localStorage.setItem('userToken', yourToken)//if you are sending token. 
    console.log(localStorage.getItem('userToken'));
});

Node.js在服务器端工作,因此客户端localStorage在这里工作。如果您将表单发布到节点(快速)端。

app.post('/getitnow', (req,res)=> {
        var LocalStorage = require('node-localstorage').LocalStorage;
        var localStorage = new LocalStorage('./scratch');
        localStorage.setItem('userToken', req.body.userToken)//if you are sending token. 
        console.log(localStorage.getItem('userToken'));
    });

并且您的帖子表格看起来像这样。

    <html>
    <body>
    <form method="post" action="/getitnow">
    <input type="hidden" name="userToken" value="eyGjasdkfasdfe4eafafsf0..."> //name should be must
    </form>
    </body>
    </html>