Get ready for production. Replace console.log with structured logging via Winston, log HTTP requests with Morgan, and keep your app alive and restarting with pm2.
Why: Winston is a popular logger. It supports levels, timestamps, and "transports" — destinations like the console or a file. JSON output makes logs easy for tools to search.
$ pnpm add winston// app.js
import winston from 'winston'
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'app.log' }),
],
})
logger.info('Server started', { port: 3000 })
logger.error('Something failed', { code: 500 })Why: Morgan is Express middleware that automatically logs every incoming HTTP request — method, URL, status code, and response time. Drop it in and you get a record of all traffic for free.
$ pnpm add morgan// app.js
import express from 'express'
import morgan from 'morgan'
const app = express()
app.use(morgan('dev')) // logs: GET /users 200 5msWhy: if your server crashes at 3am, "node server.js" stays dead. pm2 is a process manager that restarts your app automatically on crash, runs it in the background, and can spread it across CPU cores. Install it globally.
$ pnpm add --global pm2pm2 start server.js --name my-apipm2 list # see running appspm2 logs my-api # view its logspm2 restart my-api