POS_node/middleware/log.js

29 lines
774 B
JavaScript
Raw Normal View History

2024-08-27 08:37:01 +08:00
import Log from "../models/Log.js";
const logApiCall = async (req, res, next) => {
try {
// Assuming verifyToken middleware attaches the user info to req.user
const userId = req.user.id;
// Create a log entry
const logEntry = new Log({
2024-08-27 17:49:07 +08:00
userId: req.body.created_by,
2024-08-27 08:37:01 +08:00
method: req.method,
endpoint: req.originalUrl,
body: req.body,
});
// Save the log entry to the database
await logEntry.save();
// Proceed to the next middleware or route handler
next();
} catch (error) {
console.error("Error logging API call:", error);
// Optionally, you can handle the error here (e.g., send an error response)
next(error); // Pass the error to the error-handling middleware
}
};
export default logApiCall;