0

I have a route in mock-server like this: http://localhost:1337/mock-store/admin/store-parameters/1 which returns back a json:

{
  "id": 1,
  "code": "123",
  "redirect": true
}

I want that if the json includes "redirect = true", the route will redirect me to a different link.

I'm trying to use middleware.js inside my yotpo-mock server, for example:

module.exports = (req, res, next) => {
  req.header('X-Hello', 'Goodbye');
  res.header('X-Hello', 'World');
  next("www.google.com)
}

but the json response simply changed to "google.com". I want my route to be redirected to google.com.

HELP?

Loren
  • 95
  • 10

1 Answers1

1

You can use res.redirect. But in order to redirect to a completely different website you must provide a fully qualified url (with the protocol and everything)

res.redirect('http://google.com');

Tried this POC and it works:

const app = require('express')();
app.use((req,res,next)=>{
    res.redirect('https://google.com');
})
app.get('/', (req,res)=>{
    res.send('Hello');
})
app.listen(8089, ()=>{
    console.log('Running server');
})

Refer here: https://expressjs.com/en/api.html#res.redirect

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35