Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Nodejs to create middleware for access logging

Shulou Source: shulou.com Published: 2022-06-03 10:51:08 11月15日 Update

How to use Nodejs to create access logging middleware, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Middleware-access Log

target

Using middleware technology, write a middleware used to record access logs.

Middle ware

Middleware, middleware is a special url address handling function, which is used as a parameter of app.use (middleware function) or in a routing function.

Middleware is not only the biggest feature of express, but also the most important design. Express is a web development framework with minimal function, routing and middleware: in essence, an Express application is calling all kinds of middleware. [recommended: "nodejs tutorial"]

An express application is completed by many middleware.

Basic use of middleware

/ / named function format: const handler1 = (req, res, next) = > {console.log (Date.now ()); next ();} app.use (handler1); / / Anonymous function format: app.use ((req, res, next) = > {console.log (Date.now ()); next ();})

Description: there are three basic parameters in the middleware function, req, res, next

Req is the object related to the request, which is an object with the req object in the next middleware function.

Res is the object related to the response, which is an object with the res object in the next middleware function.

Next is a function, and calling it will jump out of the current middleware function and execute the subsequent middleware; if you don't call next or execute res.end, the entire request will get stuck in the current middleware.

Train of thought

Apply middleware technology to record every request

Create a .json file to store records

Put the data into the .json file after reading

Js implementation code

Const express = require ("express"); const app = express (); const fs = require ("fs"); / / get the ip function function getClientIp (req) {return (req.headers ["x-forwarded-for"] | req.connection.remoteAddress | req.socket.remoteAddress | | req.connection.socket.remoteAddress);} / / Middleware app.use ((req, res, next) = > {console.log ("time", new Date ()) Console.log (access address, req.url); console.log (f access ip, getClientIp (req)) / * * create a .json file [] * get the file content'[]'= > [] * let obj = {time:xxx,url:xxx,ip:xxx} * [] .push (obj) * [] .push (obj) overwrite the file written to .json * / fs.readFile ("hhhh.json", "utf8", (err) Data) = > {if (err) {console.log ("File read error", err) Return;} let arr = JSON.parse (data); / / console.log (arr); let obj = {}; obj.time = new Date (); obj.url = req.url; obj.ip = getClientIp (req); arr.push (obj); console.log (arr); let newArr = JSON.stringify (arr) Fs.writeFile ("hhhh.json", newArr, (err) = > {if (err) {console.log ("write error", err);});}); next ();}); / / listening interface app.listen (8080, () = > {console.log ("successful, interface is 8080");})

Console screenshot

After reading the above, have you mastered how to use Nodejs to create access logging middleware? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Middleware functions objects files logs content that is applications parameters addresses technologies interfaces methods more formats routing errors problems processing special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Huawei Linux NVidia