export json file

Asked

Viewed 366 times

1

I’m using nodejs, Mongoose.

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

I need to access my Json information from the ID. As of now, 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 only open from it.

http://localhost:3000/clientesjson/43546575867gg556TT7

1 answer

3

That one :id will be accessible on req.params.id. So you have to create a middleware that uses that 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 what you want is to check if a URL matches 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');
}
  • Just ask a question. I am doing this so I can automatically fill in my inputs after setting a field(select). With this I create a jquery, which I detailed in the following link: http://answall.com/questions/69528/load-campos-automaticamente-com-jquery-e-json?noredirect=1#comment142253_69528 .... This would be the best way, or there is another simpler way to create a Json for this?

  • @Nodejs I’m going to follow this question about how to create JSON in the other question so we don’t mix questions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.