const Koa = requirekoa;const app = new Koa; loggerappuseasync ctx next = await next; const rt = ctxresponsegetX-Response-Time; consolelog$ctxmethod $ctxurl - $rt;; x-response-timeappuseasync ctx n
这段代码使用了Koa框架建立了一个简单的HTTP服务器,并设置了三个中间件(middleware):
-
logger中间件:记录请求的方法、URL以及响应时间。
-
x-response-time中间件:记录请求的开始时间,并在响应头中添加X-Response-Time字段来表示请求所花费的时间。
-
response中间件:返回"Hello World"作为响应体。
最后使用app.listen(3000)来监听3000端口,使服务器在该端口上运行。
原文地址: https://www.cveoy.top/t/topic/X1O 著作权归作者所有。请勿转载和采集!