// plain
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.
onelinerhub: How can I use Express.js and Winston together to create a logging system?