export json file

1

I'm using nodejs, mongoose.

No controllers at the end I give res.json(clientes); and in my route I create:
app.get('/clientesjson/:id', isLoggedIn, cliente.clientesjson);

I need to access my Json information by ID. In the current way, any information I type in the ID location will open my Json file If I type:

http://localhost:3000/clientesjson/qualquercoisa

Open the Json file.

I want to know how to set an id and just open it.

http://localhost:3000/clientesjson/43546575867gg556TT7
    
asked by anonymous 16.06.2015 / 22:28

1 answer

3

This :id will be accessible in req.params.id . So you have to create middleware that uses this information and responds accordingly.

For example:

//middleware
function usarId(req, res, next){
    var id = req.params.id;
    // fazer algo aqui com o ID
    // por exemplo:
    res.locals.id = id;
}

app.get('/clientesjson/:id', isLoggedIn, usarId, cliente.clientesjson);

If you want to check if a URL is right with a certain ID then you can do it in middleware:

var userID = 'xpto';
function verificarId(req, res, next){
    var id = req.params.id;
    if (id != userID) res.status(401).send('Esse ID não está correto');
}
    
16.06.2015 / 22:41