Javascript nodejshttp服务器度量

Javascript nodejshttp服务器度量,javascript,node.js,Javascript,Node.js,作为一项任务,我要做一个nodejshttp服务器,上面有某种形式的度量 对于此任务,我不允许使用任何形式的外部库&作为请求的度量,我将存储响应时间。 对于路由处理,我将使用一个简单的switch语句来比较请求的URL 问题是,如果我想用一个或少量的请求来做这个应用程序,这个方法就可以了, 然而,在任何其他正常情况下,这对于这些指标的任何进一步更改都是可怕的&甚至更多 我正在考虑使用某种事件侦听器来解决这个问题,我会在请求的开始和结束时调用这些侦听器。 在开始时存储有关请求的信息的东西&在结束时

作为一项任务,我要做一个nodejshttp服务器,上面有某种形式的度量

对于此任务,我不允许使用任何形式的外部库&作为请求的度量,我将存储响应时间。

对于路由处理,我将使用一个简单的switch语句来比较请求的URL

问题是,如果我想用一个或少量的请求来做这个应用程序,这个方法就可以了, 然而,在任何其他正常情况下,这对于这些指标的任何进一步更改都是可怕的&甚至更多

我正在考虑使用某种事件侦听器来解决这个问题,我会在请求的开始和结束时调用这些侦听器。 在开始时存储有关请求的信息的东西&在结束时启动事件以停止处理度量

server.on('end', (data)=>{
   //end metrics somehow
})
虽然这似乎有点难以实现,尤其是我不想覆盖nodeJS事件来添加一些数据(例如,我可能想添加请求的id或时间戳)


有没有一种方法可以正确地使用nodeJS HTTP实现这一点?

如果您不能使用像express或koa这样的框架,请在每个路由上创建您自己的中间件

注:这是最简单的例子,但它提供了研究的线索

class Middleware {
    use(func) {
        this.next_func = (
          (stack) =>
            (next) =>
              stack(
                func.bind(this, next.bind(this)),
              )
        )(this.next_func);
    }
    next_func = (next) => next();
}

如果您不能使用像express或koa这样的框架,请在每个路由上创建您自己的中间件

注:这是最简单的例子,但它提供了研究的线索

class Middleware {
    use(func) {
        this.next_func = (
          (stack) =>
            (next) =>
              stack(
                func.bind(this, next.bind(this)),
              )
        )(this.next_func);
    }
    next_func = (next) => next();
}

您可以通过
res
对象的
finish
事件来处理它。当您调用
res.end()
(或类似的东西)时,将调用该事件

我的建议是,要度量API服务,您需要的信息比响应时间更多

const http = require("http");

var metrics = []

http.createServer((req, res) => {
  const startPoint = Date.now();
  res.on("finish", () => {
    if (req.url === "/metrics") {
      return; // no metrics for metrics route
    }
    metrics.push({
      path: req.url,
      method: req.method,
      status: res.statusCode,
      dateTime: startPoint,
      times: Date.now() - startPoint,
    });
  });

  switch (req.url) {
    case "/route":
      // some code to process the request, maybe some requests to the database, maybe retrieve some files etc
      setTimeout(() => {
        res.end();
      }, 1000);
      break;
    case "/metrics":
      res.setHeader('Content-Type', 'application/json');
      res.write(JSON.stringify(metrics));
      res.end()
      break;
    default:
      res.statusCode = 404;
      res.end();
      break;
  }
}).listen(8080, () => console.log("Server running!"))
如您所见,当您调用
GEThttp://localhost:8080/metrics
api,响应如下所示:

[
  {
    "path": "/test",
    "method": "GET",
    "status": 404,
    "dateTime": 1613631702906,
    "times": 1
  },
  {
    "path": "/route",
    "method": "GET",
    "status": 200,
    "dateTime": 1613631702906,
    "times": 1004
  }
]

您可以通过
res
对象的
finish
事件来处理它。当您调用
res.end()
(或类似的东西)时,将调用该事件

我的建议是,要度量API服务,您需要的信息比响应时间更多

const http = require("http");

var metrics = []

http.createServer((req, res) => {
  const startPoint = Date.now();
  res.on("finish", () => {
    if (req.url === "/metrics") {
      return; // no metrics for metrics route
    }
    metrics.push({
      path: req.url,
      method: req.method,
      status: res.statusCode,
      dateTime: startPoint,
      times: Date.now() - startPoint,
    });
  });

  switch (req.url) {
    case "/route":
      // some code to process the request, maybe some requests to the database, maybe retrieve some files etc
      setTimeout(() => {
        res.end();
      }, 1000);
      break;
    case "/metrics":
      res.setHeader('Content-Type', 'application/json');
      res.write(JSON.stringify(metrics));
      res.end()
      break;
    default:
      res.statusCode = 404;
      res.end();
      break;
  }
}).listen(8080, () => console.log("Server running!"))
如您所见,当您调用
GEThttp://localhost:8080/metrics
api,响应如下所示:

[
  {
    "path": "/test",
    "method": "GET",
    "status": 404,
    "dateTime": 1613631702906,
    "times": 1
  },
  {
    "path": "/route",
    "method": "GET",
    "status": 200,
    "dateTime": 1613631702906,
    "times": 1004
  }
]

你的想法是对的。在调用
度量时,您可以始终将其放在switch语句之前。老实说,使用像Express这样的堆栈,我们可以在其中使用中间件,所有这些都会更容易。你可以用自己的代码重新创建它。。。这对基础来说并不太复杂。你的想法是对的。在调用
度量时,您可以始终将其放在switch语句之前。老实说,使用像Express这样的堆栈,我们可以在其中使用中间件,所有这些都会更容易。你可以用自己的代码重新创建它。。。这对于基础来说并不太复杂。