最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fastify redirect to external URL - Stack Overflow

programmeradmin0浏览0评论

Using fastify library I want to be redirected to a web-page. Looking at all the examples in docs, it seems like it covers only route-to-route redirection.

Link to redirect docs

fastify.post('/v1/launch', async (req, res) => {
  response.redirect('/empty-cart');
});

The code above results in 404 error trying to find that route.

But how it can be redirected to external page? Thanks!

Update: Trying to redirect to

with ERR_NAME_NOT_RESOLVED error

Using fastify library I want to be redirected to a web-page. Looking at all the examples in docs, it seems like it covers only route-to-route redirection.

Link to redirect docs

fastify.post('/v1/launch', async (req, res) => {
  response.redirect('/empty-cart');
});

The code above results in 404 error trying to find that route.

But how it can be redirected to external page? Thanks!

Update: Trying to redirect to https://my-domain./empty-cart

with ERR_NAME_NOT_RESOLVED error

Share Improve this question edited Aug 9, 2021 at 12:22 poltorin asked Aug 9, 2021 at 12:06 poltorinpoltorin 3022 gold badges4 silver badges13 bronze badges 7
  • response.redirect('https://example.'); – 0stone0 Commented Aug 9, 2021 at 12:11
  • 1 @0stone0 question is updated. still not working – poltorin Commented Aug 9, 2021 at 12:23
  • If you enter my-domain./empty-cart in your browser do you get anything or a 404? – Frazer Commented Aug 9, 2021 at 13:18
  • @Frazer look update above – poltorin Commented Aug 9, 2021 at 13:20
  • I mean if you go to https : //my-domain./empty-cart directly, not via a redirection. – Frazer Commented Aug 9, 2021 at 13:29
 |  Show 2 more ments

2 Answers 2

Reset to default 1

Please try

response.redirect('https://stackoverflow./')

Docs: https://www.fastify.io/docs/latest/Reference/Reply/#redirectcode--dest

In nest js i have tried this way with statuscode of redirection(301) and it worked for me without statucode 301 it was not redirect as there is some open ticket on fastify-nestjs.

  @Post('success')
  async payment_success_handler(@Req() req , @Res() res , @Body() body ) {
    console.log(req.headers);
    console.log("FormData log"  , req.body)
    console.log("@Body"  , body)
    return res.redirect(301 ,'http://example./success');

  }
发布评论

评论列表(0)

  1. 暂无评论