Get user browser data from the request

In php there is a global $_SERVER['HTTP_USER_AGENT'] to get user browser data. Is there a similar function in SailsJS at the moment ?

If you are interested in User agent on server side, you can get that data from request's HTTP headers:

  • An object with all request headers: req.headers
  • String with user agent request header: req.headers['user-agent'] e.g. Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
  • String with user agent request header: req.get('user-agent') - that works exactly the same way (gives you the same result) as above expression.

Please note that the header name needs to be user-agent (lowercase characters) and not User-Agent.

This sample controller will return JSON with your current browser's user agent

module.exports = {

  /**
   * `CustomController.getUserAgent()`
   */
  getUserAgent: function (req, res) {
    return res.json({
      userAgent: req.headers['user-agent']
    });
  }
}

Example of JSON response:

{
    "userAgent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"
}

You can get the user agent information from any browser with JavaScript by calling navigator.userAgent