我如何在Node.js中使用像php的session_id()这样的东西?

时间:2016-09-03 05:28:59

标签: javascript node.js express

我使用此代码:

/*
 * dependencies
 * */
var datetime = require('node-datetime'),
    express = require('express'),
    http = require('http'),
    upload = require('jfum');

// configuration
var dirs = require('./config').directors;

// varibles
var client = 'http://localhost';

// express setup
var app = express();

// Add headers
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', client);
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-CSRF-Token,X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);

    next();
});

app.use('/upload', function(req, res, next) {
    var date = datetime.create(),
        patch = date.format('/Y/m/d/') + req.sessionID;

    upload.fileHandler({
        tmpDir: dirs.temp,
        uploadDir: __dirname + dirs.location + patch,
        uploadUrl: dirs.location_url + patch,
        maxPostSize: 11000000000, // 11 GB
        minFileSize: 1,
        maxFileSize: 10000000000, // 10 GB
        acceptFileTypes: /\.(gif|jpe?g|png)$/i,
        accessControl: {
            allowOrigin: client,
            allowMethods: 'OPTIONS, HEAD, GET, POST, PUT, DELETE'
        }
    })(req, res, next);
});

// bind event
upload.on('end', function(fileInfo) {
    // insert file info
    console.log("files upload complete");
    console.log(fileInfo);

    // var exec = require('child_process').exec;
    // var command;
    //
    // command = exec('ipconfig4', function(error, stdout, stderr) {
    //     console.log('stdout: ' + stdout);
    //     console.log('stderr: ' + stderr);
    //     if (error !== null) {
    //         console.log('exec error: ' + error);
    //     }
    // });
});

upload.on('delete', function(fileName) {
    // remove file info
    console.log("files remove complete");
    console.log(fileName);
});

upload.on('error', function(e) {
    console.log(e.message);
});



// Configuration
app.configure(function() {
    app.set('port', process.env.PORT || 3001);

    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser('token'));
    app.use(express.session({
        secret: 'secret'
    }));
    app.use(express.favicon());
    app.use(express.static(__dirname + '/public'));
});

app.configure('development', function() {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
    app.set('view cache', false);
});

app.configure('production', function() {
    app.use(express.errorHandler());
    app.set('view cache', true);
});

/*
 * routes
 * */
app.get('/', function(req, res) {
    res.send(req.sessionID);
});

/*
 * start server
 * */
http.createServer(app).listen(app.get('port'), function() {
    console.log("Express server listening on port " + app.get('port'));
    console.log("access url /location/input");
});

在路线中,似乎我可以使用req.sessionID获得类似php的session_id()。但是在

app.use('/upload', function(req, res, next) {
    var date = datetime.create(),
        patch = date.format('/Y/m/d/') + req.sessionID;
});

我从undefined获得req.sessionID。为什么?这是否像php的session_id()一样工作?

2 个答案:

答案 0 :(得分:1)

开箱即用的Express没有sessionID变量。您需要使用https://github.com/expressjs/session之类的中间件,它将为您提供req.sessionID。有关详细信息,请参阅https://github.com/expressjs/session#reqsessionid-1

答案 1 :(得分:0)

节点网络服务器开发在中间件中运行。您需要做的是找到适当的会话中间件,并在调用req.sessionID()之前将其输入到中间件堆栈。