https://claude.ai/share/2bbcedbe-1cbc-4b4a-b57f-5f1c1ffeaf01
Для запуска приложения с помощью PM2 в режиме кластера с 2 инстансами и объединением логов:
npm install -g pm2pm2 start bin/www --name "test-app" -i 2 --merge-logspm2 status
pm2 logspm2 stop test-app
pm2 delete test-appbin/www- точка входа приложения--name "test-app"- имя процесса в PM2-i 2- количество инстансов (2)--merge-logs- объединение логов всех инстансов
curl http://localhost:3000-
Для конкретного роута в routes/index.js: // Middleware только для /hello router.get('/hello', (req, res, next) => { const timestamp = new Date().toISOString(); console.log(
[${timestamp}] ${req.method} ${req.url}); next(); }, function(req, res) { res.json({ message: "Hello, World!" }); } ); -
Для нескольких рутов: // Создать функцию middleware function logRequest(req, res, next) { const timestamp = new Date().toISOString(); console.log(
[${timestamp}] ${req.method} ${req.url}); next(); }
// Применить к нужным рутам router.get('/hello', logRequest, function(req, res) { /* ... / }); router.get('/greet', logRequest, function(req, res) { / ... */ });
- Для всех рутов определенного типа:
// Только для GET запросов
router.use((req, res, next) => {
if (req.method === 'GET') {
const timestamp = new Date().toISOString();
console.log(
[${timestamp}] ${req.method} ${req.url}); } next(); });