简体   繁体   中英

Node JS Express Ports

I am using redis to send a session cookie from PHP to Node/Express. The only way I seem to be able to access the cookie in my server.js code is if I create an empty file on the client and include it like this with the port:

<script type="text/javascript" src="http://dev.nodetest.com:8080/empty.js"></script>

If I don't include this on the client, I cannot access the cookie data on the node server.

My requirement is that I don't need to send the cookie back to the client. Only to access it in the server.js to authenticate a user against the DB.

Here is the contents of my server.js file:

var express = require('express'),
    http = require('http'),
    app = express(),
    server = http.createServer(app),
    io = require('socket.io').listen(8081),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    RedisStore = require('connect-redis')(session);

app.use(cookieParser());

app.use(session({
    store: new RedisStore({
        // this is the default prefix used by redis-session-php
        prefix: 'session:php:'
    }),
    // use the default PHP session cookie name
    name: 'PHPSESSID',
    secret: 'node.js rules'
}));

app.use(function(req, res, next) {
    console.log(req.session);
    req.session.nodejs = 'Hello from node.js!';
    res.send();
});

io.sockets.on('connection', function (socket) {
    socket.on('subscribe', function(room) {
        console.log('joining room', room);
        socket.join(room);
    });
});

app.listen(8080);

You are printing the session info in the express middleware below:

app.use(function(req, res, next) {
    console.log(req.session);
    req.session.nodejs = 'Hello from node.js!';
    res.send();
});

This middleware is ONLY invoked when you make a normal http request (interpret it as express requests). When you make transactions with socket.io the above code is never executed (eg. connect a socket, send a message, disconnect, etc).

index.php generates a cookie and a page that contains a script that connects to socket.io and emits a message to it. When you do just that, the above middleware is not executed. But, when you generate a "normal request" the above middleware will be executed. That's why the middleware is executed when you include the script empty.js with:

<script type="text/javascript" src="http://dev.nodetest.com:8080/empty.js"></script>

In practice this would be executed for any request, eg. http://dev.nodetest.com:8080/foobar or http://dev.nodetest.com:8080/anotherfoobar . The url do not need to point to existing files. Any URL on port 8080 will invoke the middleware. BUT SOCKET.IO REQUESTS WILL NEVER TRIGGER YOUR TESTING MIDDLEWARE.

EDIT There are libraries that may help you working with sessions in socket.io. sessio.socket.io seems to be adequate for your scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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