Angelique Buijzen
Angelique Buijzen

Reputation: 23

Variable route/url param connected to Mongodb

Thanks for reading my question!

I'm making a chat-app where people can chat with each other who are born at the same date.

So the question is, How do I change the route with an additional birthdate which is changing with every different user with different birthdates?

I already can signup/login and save the birthdate for every user. So I need to take the birthdate from the mongoDB to put it in the url so nobody gets directed to the wrong chat.

router.get("/", chatController.getAll);

app.use('/api/v1/chat', passport.authenticate('jwt', { session: false }), apiChatRouter);

const getAll = (req,res)=>{
    Message.find({},(err,docs)=>{
        if(!err){
            res.json({
                "status":"succes",
                "data":{
                    "chat":docs
                }
            });
        }
    });
}

fetch('http://localhost:3000/api/v1/chat/', {

    //nodige headers meegeven met localstorage
    'headers':{
        'Authorization': 'Bearer ' + localStorage.getItem('token')
    }
}).then(result => {
    return result.json();
}).then(json =>{
    console.log(json);
}).catch(err =>{
    console.log("Unauthorized")
});

If you want to check the rest of the code: https://github.com/abuijzen/Herexamen-Webtech3

Upvotes: 0

Views: 300

Answers (1)

Yasser Mas
Yasser Mas

Reputation: 1682

You can use express path param please check the below example

To redirect user to other path you can user

  res.redirect('/1990-03-29');

to get param from url

app.get('/api/v1/chat/:bd', function (req, res) {
  // http://localhost:xx/api/v1/chat/1990-03-29
  req.params // eqal { "bd": "1990-03-29" }

})

Please check routing and redirect

Upvotes: 1

Related Questions