NodeJS-Express res.json and res.render

1

I am doing a mobile client for my application in NodeJS but I do not have much experience doing a REST Web Service, and I am trying to return a JSON in my application to receive it in the Android APP, but I only return a single file type, HTML or JSON but not 2. What can I do? Should I make the WebService part?

getViewIndex(req, res, next){
    //console.log(req.user.nombres);
    //console.log(req.isAuthenticated());
    if(!req.isAuthenticated())
        res.redirect('/');
    else{            
        res.render('index', { //envio el html
            title: 'Bienvenido',
            user: req.user
        });
        res.json(req.user); //envio el JSON
    }
} 

Only send the first file, change the order and always send the first one. Thanks in advance.

    
asked by Joseph Leon 13.03.2017 в 21:44
source

1 answer

0

1 Request has only 1 Response. You can not make a request and return 2 responses of different types ... If you want to return the JSON you should only do the second, the first is rendering a page and you are sending data to the Template Engine (eg Handelbars ) renderize an HTML with that data that you are passing it.

If you only want to return JSON, make the number 2.

    
answered by 14.03.2017 в 18:29