Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nodejs to create middleware for access logging

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report