expressjsHow can I use Express.js and Winston together to create a logging system?
Express.js and Winston can be used together to create a logging system. The following example code will demonstrate how to do this:
const express = require('express');
const winston = require('winston');
// Create the Express app
const app = express();
// Configure Winston to log to the console
const logger = winston.createLogger({
transports: [
new winston.transports.Console()
]
});
// Set up a route that will log a message when accessed
app.get('/', (req, res) => {
logger.info('A request was made to the root route');
res.send('Hello World!');
});
// Listen on port 3000
app.listen(3000);
This code will create an Express server that listens on port 3000 and logs a message to the console when the root route is accessed.
The code consists of the following parts:
- Require the Express and Winston modules.
- Create the Express app.
- Configure Winston to log to the console.
- Set up the root route to log a message when accessed.
- Listen on port 3000.
Helpful links
More of Expressjs
- How can I use Express.js to trace requests?
- How can I use OpenTelemetry with Express.js?
- How do I set the time zone in Express.js?
- How do I use Zod with Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I use Express.js to develop a web application?
- How can I use the x-forwarded-for header in Express.js?
- How can I use Express.js to implement websockets in my application?
- How do I manage user roles in Express.js?
- What is Express.js?
See more codes...