node-api-logs
v1.1.2
Published
A package to see logs on your nodejs server (expressjs)
Downloads
13
Readme
Node API Logs
Node API Logs is a library that allows Express.js developers to view req logs on their server without the need for any third-party services or payments. from req body, ip, res body, duration, etc. The library currently only supports Express.js and requires a MongoDB URI. Supports for Nestjs and other frameworks will come later.
Features
View server logs in a user-friendly interface
Secure login system for accessing logs
Simple user management (password reset and user addition through MongoDB)
Easy integration with existing Express.js applications
Save on specific route data
Filter logs with endpoint, time, date and status.
Installation
You can install the library using npm or yarn:
npm i node-api-logs
# or
yarn add node-api-logs
To use the library, you need to import the createExpressLogger
import {createExpressLogger} from "node-api-logs";
createExpressLogger({app:app,mongoUri:process.env.MONGO_TEST_URI || process.env.MONGO_URI||""});
Next, provide your Express app instance along with a MongoDB URI.
You can make request for specific url and make to avoid some url.
createExpressLogger({app:app,mongoUri:process.env.MONGO_TEST_URI || process.env.MONGO_URI||"", beginswith:["/api"],specifics:["/api/v1/admin/info"]});
// This function will only log and save all api requests to /api but avoid request to /api/v1/admin/info
Here’s a basic example of how to use the library in your index.ts file:
import express, { Application, Request, Response } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import dotenv from 'dotenv';
import expressFileUpload from 'express-fileupload';
import path from 'path';
import {createExpressLogger} from "node-api-logs";
// configurations
dotenv.config();
import './config/database';
import './config/redis';
import AppRoutes from './modules/app/app.route';
import { formatReq } from './middlewares/helpers.middleware';
import logger from './config/logger';
import morgan from 'morgan';
import morganMiddleware from './middlewares/morgan.middleware';
// Boot express
const app: Application = express();
const port = process.env.PORT || 3000;
const base: string = process.env.base_url ?? '/staging/api/v1';
// middlewares
app.use(cors());
app.use(helmet());
app.use(expressFileUpload({ createParentPath: true, useTempFiles: true }));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use('/docs', express.static(path.join(__dirname, 'docs')));
app.use(formatReq);
app.use(morganMiddleware);
#create logger
createExpressLogger({app:app,mongoUri:process.env.MONGO_TEST_URI || process.env.MONGO_URI||""});
// Application routing
app.get('/', (req: Request, res: Response) => {
res.status(200).send({ data: 'BACKEND Application' });
});
app.use(base, AppRoutes);
// Start server
app.listen(port, () => logger.info(`Server is listening on port ${port}!`));
// Handle unhandled promise rejections and exceptions
process.on('unhandledRejection', (err: any) => {
logger.error('Unhandled Rejection', err);
});
process.on('uncaughtException', (err: any) => {
logger.error(err.message, err);
});
To access the logs, visit:
http://localhost:PORT/logs/login
User Management
To change a user's password:
Go to bcrypt-generator to generate a new password.
Manually update the user's password in the MongoDB database.
To add a new user:
Go to your
users
collection in MongoDB.Click on Add Data and select Insert Document.
Click Insert and then edit the new document to add the user's email.
Once the user visits
http://localhost:PORT/logs/login
and inputs their email and password, the password will be automatically set for them.
To remove a user:
Simply delete the user's record from the database.
Contributing
Contributions are welcome! If you'd like to contribute, please follow these steps:
Fork the repository.
Create a new branch (
git checkout -b feature-branch
).Make your changes.
Commit your changes (
git commit -m 'Add feature'
).Push to the branch (
git push origin feature-branch
).Open a pull request.
Please make sure to update tests as appropriate.
Contact
For issues or questions, feel free to contact me at:
Email: [email protected]
GitHub: Ugochukwudev
Twitter Impulsejs
Blog [Techgix] (https://techgix.xyz)
License
This project is licensed under the MIT License. See the LICENSE
file for details.