Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browsersync middleware: Redirect from a post request

We are using browsersync in development. For a payment confirmation, the payment provider posts back to a predefined url.

The problem is, that Browsersync only displays Cannot POST /payment/success.

So I was thinking about writing a middleware that takes the POST request and redirects to the same URL as a GET request. The problem is, I'm not sure if this is possible with browser sync. When I try to call res.redirect('/') it throws an error:

    browserSync.init
        server:
            baseDir: "...."
            index: "/statics/master.html"
            middleware: [
                modRewrite(['!\\.\\w+$ /statics/master.html [L]']),
                (req, res, next) ->
                    res.redirect('/') # this throws a TypeError: res.redirect is not a function
            ]
  1. Why is res.redirect not defined? I thought browser sync is based on express?
  2. How could I access the body of the post request? req.body returns undefined
like image 282
23tux Avatar asked Oct 24 '25 16:10

23tux


1 Answers

browserSync uses Connect for the middleware and doesn't share the same api as Express it seems.

For a redirect you can at least do the following:

res.writeHead(301, {Location: url});
res.end();

Have a look at their documentation for accessing the request body. The section in Middleware regarding body parsing may yield some results for you.

like image 86
OACDesigns Avatar answered Oct 26 '25 06:10

OACDesigns