Morgan.js 介绍
Morgan是一个记录HTTP请求和错误的中间件。中间件是一个简单的函数,它可以访问请求和响应的生命周期方法。我们可以使用预定义的格式或创建新的格式来格式化我们的日志。
安装Morgan的步骤:
步骤1: 你可以访问Morgan的文档。可以使用以下命令安装该软件包。
npm install morgan
步骤2: 安装完成后,您可以使用以下命令检查已安装的软件包版本:
npm ls morgan
步骤3: 要开始使用morgan,需创建一个名为index.js的文件。可以使用以下命令将包包含在index.js中。
const morgan = require('morgan')
步骤 4: 要使用morgan,我们必须调用一个实例并将其作为参数传递给app.use(),它是一个在HTTP请求之前的express中间件。可以按如下方式完成:
app.use(morgan(string));
上面的字符串定义了我们想要记录信息的格式。
项目结构: 项目的结构将如下图所示。
Morgan有五种预定义的格式,我们可以直接使用这些格式来获取所需的信息。这些格式包括:
- combined:它提供了Apache标准的combined格式日志。
- common:它提供了标准Apache common日志输出。
- dev:它是一个带有颜色编码的日志格式。
- short:它比默认格式更短,还包括响应时间。
- tiny:它是最简短的日志,包含很少的信息。
示例1:
index.js
// Requiring modules
const express = require("express");
const morgan = require("morgan");
const app = express();
// Using combined predefined format
app.use(morgan("combined"));
// Creating an endpoint where
// requests can be made and
// we can get logs
app.get("/", (req, res) => {
res.send("GeeksforGeeks");
});
// Running on PORT
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Running on PORT: ${PORT}`);
});
运行应用的步骤:
在终端中运行以下命令来执行 index.js 文件。
node index.js
输出结果:
::1 - - [10/Feb/2021:22:18:30 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/88.0.4324.150 Safari/537.36"
当你访问https://localhost:5000时,上述日志将出现在控制台中。此日志提供了各种信息,例如请求的日期和时间、请求的方法(在此案例中为GET)、请求的URL(为’/’),以及更多其他信息。
示例2:
index.js
// Requiring modules
const express = require("express");
const morgan = require("morgan");
const app = express();
// Using dev predefined format
// Returns
app.use(morgan("dev"));
// Creating an endpoint where
// requests can be made and
// we can get logs
app.get("/home", (req, res) => {
res.send("GeeksforGeeks");
});
// Running on PORT
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Running on PORT: ${PORT}`);
});
输出:
当你访问“https://localhost:5000”时,输出结果将是:
GET / 404 4.031 ms - 139
由于我们没有针对“/” URL的任何端点,所以我们以GET方法和响应时间(以毫秒为单位)最终得到404错误。
当您访问“https://localhost:5000/home”时,输出将为:
GET /home 200 3.416 ms - 13
在我们的程序中,我们创建了一个端点“/home” ,所以我们得到了一个200的响应。
示例3:
index.js
// Requiring modules
const express = require("express");
const morgan = require("morgan");
const app = express();
// Creating customised logs
// :method return method used like - GET,POST
// :url returns to which url request was made
// :status returns the status returned from req like - 200,404
// :response-time returns the time it took to give response
// Rest things are printed as it is
app.use(
morgan(
"Method- :method URL- :url Status- :status ResponseTime- :response-time ms"
)
);
// Creating an endpoint where
// requests can be made and
// we can get logs
app.get("/home", (req, res) => {
res.send("GeeksforGeeks");
});
// Running on PORT
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Running on PORT: ${PORT}`);
});
输出:
Method- GET URL- /home Status- 200 ResponseTime- 3.392 ms
在上面的程序中,我们自定义了输出日志的格式。格式是: 后跟信息名称。这些是预定义的标记,可以通过在它们前面使用: 来添加。 我们还可以通过使用.token()方法创建自定义标记。参见下面的示例:
示例4: index.js
// Requiring modules
const express = require("express");
const morgan = require("morgan");
const app = express();
// Creating custom token
// with name time
morgan.token(
"time",
" :method request for :url was received.Response time: :response-time"
);
// Using the name of
// token we created above
app.use(morgan("time"));
// Creating an endpoint where
// requests can be made and
// we can get logs
app.get("/home", (req, res) => {
res.send("GeeksforGeeks");
});
// Running on PORT
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Running on PORT: ${PORT}`);
});
输出:
GET request for /home was received.Response time: 5.567
Morgan是一个非常简单的日志记录器,可以在记录HTTP请求时提供灵活性并为我们提供所需的信息。因此,这是我们如何在我们的应用程序中使用morgan