3

I'm trying to add Node.js to my web application in Django to have real time. I'm doing the functionality of "like" as in facebook, when users click "like" AJAX will post the ID of song to domain.com:3000 (Node.js server).

I have the user ID on a session variable, but i don't know how to access to this session variable from Node.js.

Here is my code:

Node.js

var express = require('express');

var app = express();

app.use(express.static('./public'));
app.use(express.bodyParser());
app.use(express.cookieParser());

app.get('/', function(req, res){
    res.send("ID user: " + req.session.user_id);
});

app.listen(3000);
console.log("Working under 3000")

Django

def auth(request):
    if request.is_ajax() and request.method == 'POST':
        email = request.POST.get('username', '')
        password = request.POST.get('password', '')
        autenti = JayapalBackend()
        auth = autenti.authenticate(email, password)

        if auth is not None:
            id_usuario = Usuario.objects.get(email=email).getId()
            request.session['user_id'] = id_usuario
            return HttpResponse('1')
        else:
            return HttpResponse("-1")
    else:
        return HttpResponse("-1")   

Thanks

EDITED:

I get this error:

TypeError: Cannot read property 'user_id' of undefined
4

0 回答 0