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 do I use Express.js to handle asynchronous requests?
- How do I use adm-zip with Express.js?
- How can I use Express.js to generate a zip response?
- How can I use Zipkin to trace requests in Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I set up X-Frame-Options in ExpressJS?
- How can I use Express.js and Vite together for software development?
- How do I download a zip file using Express.js?
- How do I write unit tests for ExpressJS?
- How do I render a template using Express.js?
See more codes...