NodeJS + Express:如何在其他方法中使用会话变量。 (会话变量:未定义)

时间:2013-12-09 11:48:41

标签: node.js session express session-variables

我正在使用NodeJS 0.10.18和Express 3.4.0来使用Web服务器来处理PhoneGap应用程序。

我想使用Session Variables / Sessions来存储用户用户名,以便在其他方法中使用它。

当我登录登录方法时,用户名存储在 req.session.name 中(设置,因为我可以将其打印到控制台)。但是后来当我想在保留方法中使用它时, req.session.name 变量突然未定义。

你能告诉我我做错了吗?

这是我的代码:

//NodeJS Modules
var http = require('http');
var express = require("express");

//Variables
var app = express();
var listenport = '8123';
var Username = "";
var Password = "";


//Application configuration
app.configure(function(){
    app.use(express.cookieParser('S3CRE7'));
    app.use(express.cookieSession());
    app.use(app.router);
});


//Create header for every HTTP response
app.all("*", function(req, res, next) {
    res.writeHead(200, {'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*'});
    next();
});

//Login
app.post("/MobileApplication/login", function(req, res) {
    //Response 0 = correct

    var loginUsername = req.param('Username');

    //VALIDATE 

    //STORE IN SESSION
    req.session.name = loginUsername;
    console.log(req.session.name);

    res.end("0");
});


//Reservation
app.post("/MobileApplication/reserve", function(req, res) {
    var name = req.session.name;
    console.log(name);
    res.end("0");
});

//If the url isn't valid respond with 404
app.all("*", function(request, response) {
    response.end("404!");
});

//Listen on listenport
http.createServer(app).listen(listenport);

Phonegap ajax登录请求:

jQuery.ajax({
            url: "http://" + serverip + ":" + serverpoort + "/MobileApplication/login?Username=" + Username + "&Password=" + Password + "",
            type: "POST",
            beforeSend: function(data) {
                console.log("login - sending");
            },
            success: function(data) {
                console.log("login - succes!");

                if (data === "0") {
                    $('input[type="checkbox"]').filter('#checkbox-1').each(function() {
                        if ($(this).is(':checked')) {
                            // perform operation for checked
                            window.localStorage.setItem("Username", Username);
                            window.localStorage.setItem("Password", Password);
                            $.mobile.navigate("#customers", {transition: "slide"});
                        }
                        else {
                            // perform operation for unchecked
                            window.localStorage.setItem("Username", "");
                            window.localStorage.setItem("Password", "");
                            $.mobile.navigate("#customers", {transition: "slide"});
                        }
                    });
                }
                if (data === "1") {
                    navigator.notification.alert(
                            'The username you have entered does not exist.', // message
                            doNothing,
                            'Error', // title
                            'Ok'                  // buttonName
                            );
                }
                if (data === "2") {
                    navigator.notification.alert(
                            'You have entered a wrong password.', // message
                            doNothing,
                            'Error', // title
                            'Ok'                  // buttonName
                            );
                }

            },
            error: function(a, b, c) {
                alert("login - error: " + a.toString() + " " + b.toString() + " " + c.toString());
                navigator.notification.alert(
                        "login - error: " + a.toString() + " " + b.toString() + " " + c.toString(), // message
                        doNothing,
                        'Error', // title
                        'Ok'                  // buttonName
                        );
            }
        });

0 个答案:

没有答案