Heroku doesn't allow redirect

Hi Campers,
I face a problem with Heroku. I set database and deploy my nodes app ( URL Shortener Microservice ) then the site didn’t redirect to the site, Although it redirect to the specific URL from database on my localhost

https://mahmoud-url-shortener.herokuapp.com

app.get('/:id', ( req, res, next ) => {
    URL.findOne({ _id: req.params.id }).then( url => {
      if( url ) res.redirect( url.link ) 
      else res.send({ error: "This url is not on the database." })
    })
})

{“orginal_url”:“https://github.com”,“url”:“http://www.mahmoud-url-shortener.herokuapp.com/BJqGxgMwW”}

i get this when i try eg using https://github.com … problem i see is i get http as the start of the url not https
so could this be your problem

1 Like

It looks like you’re asking for the wrong property.

When you create the short link, it makes two properties, orginal_url (original is spelled funny there :wink: ) and url. But in the route above, you’re looking for link.

Try changing the route to:

app.get('/:id', ( req, res, next ) => {
    URL.findOne({ _id: req.params.id }).then( url => {
      if( url ) res.redirect( url.orginal_url ) 
      else res.send({ error: "This url is not on the database." })
    })
})
1 Like

thanks, guys, but the problem is solved by remove www. before the shorten URL and it works well