更新时间:2023-12-01 22:40:46
您可以通过 Sails policy 通过在 req.options
上设置一些属性.如果您有一个 User
模型并且正在使用蓝图 create
路由,那么在您的 config/policies 中,您将拥有:
You can do this via a Sails policy by setting some properties on req.options
. If you have a User
model and are using the blueprint create
route, then in your config/policies you'd have:
UserController: {
create: 'setValues'
}
在 api/policies/setValues.js 中:
module.exports = function(req, res, next) {
req.options.values = req.options.values || {};
req.options.values.ip = <SET IP>;
req.options.values.agent = <SET USER AGENT>;
return next();
};
我不记得获取用户 IP 的首选方式,但是 这个问题 看起来很有希望.对于用户代理,您可以尝试 req.headers['user-agent']
.
I don't remember the preferred way to get user IP, but this question looks promising. For user agent you can try req.headers['user-agent']
.
如果您使用的是自定义控制器操作而不是蓝图,这仍然可以正常工作,您只需要将随请求传递的值与 req.options.values
合并.
If you're using a custom controller action rather than the blueprints, this will still work fine, you'll just need to merge the values passed with the request with req.options.values
.