Browsersync 中间件:从 post 请求重定向

Browsersync middleware: Redirect from a post request

我们正在开发中使用 browsersync。对于付款确认,付款提供商回发到预定义的 url.

问题是,Browsersync 只显示 Cannot POST /payment/success

所以我在考虑编写一个中间件,它接受 POST 请求并重定向到与 GET 请求相同的 URL。问题是,我不确定浏览器同步是否可行。当我尝试调用 res.redirect('/') 时,它会抛出一个错误:

    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

browserSync 使用 Connect 作为中间件,并不像 Express 那样共享相同的 api。

对于重定向,您至少可以执行以下操作:

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

查看有关访问请求正文的文档。 Middleware 中关于正文解析的部分可能会为您产生一些结果。