如何在节点js中记录执行的控制器和操作?

时间:2019-03-07 12:06:48

标签: node.js express

nodeexpress中是否有一种方法可以记录调用controller时哪个action和哪个api被执行。

Ruby on rails中,当调用api时,将记录已执行的Railscontroller。如何在action

中实现

例如在nodejs中:

Ruby on Rails转到此URL UI,在这种情况下,某些localhost:3000/users/sign_in将在controller日志下面执行

rails

请参阅此处路径:{"method":"GET","path":"/users/sign_in","format":"html","controller":"SessionsController","action":"new","status":200,"duration":167.26,"view":107.06,"db":39.51,"timestamp":"2019-03-07T17:41:51+05:30","request_id":null,"logged_in_user_token":null,"user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36","params":["controller","action"]}

控制器:/users/sign_in

操作:SessionsController

已登录日志

1 个答案:

答案 0 :(得分:0)

我不确定快递,但我不知道有任何内置的方法可以在节点上实现这一目标。如果您构建服务器,则可以从请求对象中读取所需的参数,然后在发送适当的响应后手动注销它们:

  if (statusCode == 200) {
console.log(
  "\x1b[32m%s\x1b[0m",
  method.toUpperCase() + " /" + trimmedPath + " " + statusCode
);
  } else {
    console.log(
      "\x1b[31m%s\x1b[0m",
      method.toUpperCase() + " /" + trimmedPath + " " + statusCode
    );
    console.log(payload);
  }

然后,每当您碰到一条路线时,它将登出控制台。