pralayasimha Yedida
pralayasimha Yedida

Reputation: 33

How to pass dynamic variable into redirect URL in Express api?

I have a slug param in my API url /photo/:slug/. After it has completed, it redirects to another page which has the slug in its url. How do I insert the slug into the redirect url?

Here is my route where I am uploading a file to the server:

exports.postEntriesCollection = (req, res) => {
    Campaign.findOne({slug:req.params.slug}, function(err, campaign) {
    let filename = ''  
        if(!isEmpty(req.files)) {
            let file = req.files.file;
            filename = file.name;
            let uploadDir = './public/PhotoContestUploads/uploads/';
            console.log(filename);
            file.mv(uploadDir+filename, (err) => {
                if (err)
                    throw err;
            });
        }
        const photo = new PhotoEntries({
            Email: req.body.email,
            Name: req.body.name,
            Description: req.body.description,
            Phonenumber: req.body.Phone, 
            CampaignId: campaign._id,
            done: false,
            Photo: `uploads/${filename}`
        });
        photo.save().then(post => {
            console.log(photo);
            req.flash('success', { msg: 'Uploaded successfully' });
            res.redirect('/photoapp/**:slug**/success');
        });
    });
}

How do I do this?

Upvotes: 1

Views: 759

Answers (1)

Dan
Dan

Reputation: 63059

Since slug is just a string that you want inserted into another string, do:

res.redirect('/photoapp/' + req.params.slug + '/success');

or this if you prefer:

res.redirect(`/photoapp/${req.params.slug}/success`);

Upvotes: 1

Related Questions