Search code examples
gulpbrowser-syncnode.js-connect

what is the request object in browsersync middleware: function(req, res, next())


Browsersync mentions the following.

Does anyone know what are the properties of the request object ? e.g. How can I get the Host property of the request ?

 middleware: function (req, res, next) {
     //the following prints undefined - where can we learn about res, req and next()
     console.log(res.getHeader('Host'));
 }

Solution

  • It appears to be a regular Node Http.ClientRequest object:

    https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_class_http_clientrequest

    The 'headers' property, for example, is just a normal JS object:

    req.headers['host']
    

    should give you the host.

    Your code example above is trying to get the response header, not the request header.