How to Secure and Monitor Your Node.js App with PM2 and SecureNow
Step-by-step guide to integrating SecureNow into your Node.js Express application using PM2. Learn how to set up automatic monitoring and security threat detection.
Posted by
Related reading
How to Integrate SecureNow with Your Next.js Application
Complete guide to adding enterprise-grade security monitoring to your Next.js app with SecureNow. Learn automatic setup, manual configuration, and best practices for production deployment.

Introduction
Securing and monitoring your Node.js application in production is crucial for maintaining reliability and protecting against security threats. In this comprehensive guide, you'll learn how to integrate SecureNow with PM2, the popular Node.js process manager, to get enterprise-grade security monitoring with minimal configuration.
PM2 combined with SecureNow provides automatic restart capabilities, process monitoring, and real-time security threat detection for your Express.js applications. Let's dive into the setup process.
Video Tutorial
Watch our step-by-step video tutorial on how to secure and monitor your Node.js application with PM2 and SecureNow:
Follow along with the video or continue reading the written guide below.
Step 1: Install the SecureNow Package
Add the SecureNow package to your project using npm, yarn, or pnpm. This will enable automatic security monitoring and threat detection for your Node.js application.
npm install --save securenowThis command adds SecureNow as a dependency in your package.json file. The package includes automatic instrumentation for Express.js, database queries, HTTP requests, and common security vulnerabilities.
Step 2: Configure PM2 with SecureNow
Create Your Ecosystem Configuration
Set up your ecosystem.config.js file with SecureNow configuration. This file tells PM2 how to run your application and what environment variables to use.
Create or update your ecosystem.config.js file in your project root:
module.exports = {
apps: [{
name: "your-app-name",
script: "./app.js",
node_args: ['-r', 'securenow/register'],
env_production: {
SECURENOW_APPID: 'be3f5906-fc15-4b99-8500-7496800a54d5',
SECURENOW_INSTANCE: 'http://46.62.173.237:4318',
SECURENOW_NO_UUID: '1'
}
}]
}Configuration Parameters Explained
node_args: ['-r', 'securenow/register']
Automatically loads SecureNow at application startup using Node.js preload. This enables automatic instrumentation without requiring any code changes in your application. SecureNow will intercept HTTP requests, database queries, and other operations to monitor for security threats.
SECURENOW_APPID: '<your-api-key>'
Your unique application API key for authentication. This identifies your application in the SecureNow dashboard and ensures that only your authorized instances can send monitoring data. You can find your API key in the SecureNow dashboard under Applications → Your App → API Keys.
SECURENOW_INSTANCE: 'http://46.62.173.237:4318'
Optional: The SecureNow collector endpoint URL. Only add this if you're an enterprise customer with a dedicated instance. For most users, SecureNow will automatically use the default cloud endpoint, so you can omit this configuration variable.
SECURENOW_NO_UUID: '1'
Disables UUID generation for traces, reducing overhead and improving performance. Set to '1' to disable or remove this line to enable UUID generation. For most applications, disabling UUIDs is recommended as it reduces memory usage and network overhead without impacting functionality.
💡 Enterprise Customers
If you have a dedicated SecureNow instance, replace the SECURENOW_INSTANCE URL with your custom endpoint provided by our team. Contact support@securenow.com for your dedicated instance details.
Step 3: Start Your Application with PM2
Launch your application using PM2 with the production environment configuration. This will start your app with all the SecureNow security monitoring features enabled.
Start your application with PM2:
pm2 start ecosystem.config.js --env productionThe --env production flag ensures PM2 uses the production environment variables defined in your ecosystem.config.js file, including all SecureNow configuration.
✅ What's Next?
Once your application is running, SecureNow will automatically start collecting traces and monitoring for security threats. Visit your dashboard at dashboard.securenow.com to view real-time analytics, security alerts, and performance metrics.
Useful PM2 Commands
Here are the essential PM2 commands you'll need to manage your application:
Restart Your Application
pm2 restart your-app-nameRestarts your application with zero downtime. Useful when deploying updates or applying configuration changes.
View Application Logs
pm2 logs your-app-nameDisplays real-time logs from your application. Press Ctrl+C to exit. Add --lines 100 to see the last 100 lines of logs.
Monitor Application Metrics
pm2 monitOpens an interactive monitoring dashboard showing CPU usage, memory consumption, and other vital metrics in real-time.
Stop Your Application
pm2 stop your-app-nameGracefully stops your application without removing it from PM2's process list.
Additional Useful Commands
# List all running applications
pm2 list
# Delete application from PM2
pm2 delete your-app-name
# Save current process list
pm2 save
# Resurrect saved processes on reboot
pm2 resurrect
# Display detailed application info
pm2 show your-app-nameWhat SecureNow Monitors
Once integrated, SecureNow automatically monitors your application for:
- SQL Injection Attempts: Detects and blocks malicious SQL patterns in database queries
- XSS (Cross-Site Scripting): Identifies script injection attempts in user inputs
- Authentication Failures: Tracks failed login attempts and potential brute force attacks
- Rate Limiting Violations: Monitors for suspicious request patterns and DDoS attempts
- Unusual Traffic Patterns: AI-powered detection of anomalous user behavior
- Performance Metrics: Response times, error rates, and throughput monitoring
- Memory Leaks: Automatic detection of memory usage anomalies
- API Abuse: Identifies unauthorized API access and misuse patterns
Viewing Your Dashboard
After starting your application, log in to your SecureNow dashboard to access:
- Real-time Threat Detection: Live feed of security events and threats
- Performance Analytics: Response time graphs, error rates, and throughput charts
- Trace Analysis: Detailed request traces with AI-powered insights
- Alert Configuration: Set up custom alerts for security events and performance issues
- Forensic Analysis: Deep dive into past incidents with comprehensive trace data
Best Practices
Security
- Never commit your SECURENOW_APPID to version control. Use environment variables or secrets management
- Use different API keys for development, staging, and production environments
- Regularly rotate your API keys (recommended every 90 days)
- Enable IP whitelisting in your SecureNow dashboard for production environments
Performance
- Monitor SecureNow's overhead using pm2 monit - it should typically use less than 2% additional CPU and 50MB of memory
- Adjust sampling rates if you have extremely high traffic (contact support for guidance)
- Configure PM2 cluster mode for improved performance and reliability
Monitoring
- Set up alerts in the SecureNow dashboard for critical security events
- Review your security dashboard daily to stay on top of potential threats
- Configure Slack or email notifications for immediate threat awareness
- Use PM2's pm2 save command to persist your process list
Troubleshooting
Application Not Appearing in Dashboard
If your application isn't showing up in the SecureNow dashboard:
- Verify your SECURENOW_APPID is correct
- Check that PM2 started with --env production flag
- View logs with pm2 logs your-app-name for any SecureNow errors
- Ensure your application is receiving traffic (SecureNow only sends data when requests are made)
High Memory Usage
If you notice increased memory usage:
- Ensure SECURENOW_NO_UUID is set to '1'
- Contact support to adjust sampling rates for high-traffic applications
- Check for memory leaks in your application code (SecureNow can help identify these)
Conclusion
Congratulations! You've successfully integrated SecureNow with PM2 to monitor and secure your Node.js application. Your app is now protected with enterprise-grade security monitoring that automatically detects threats, analyzes performance, and provides actionable insights.
SecureNow works silently in the background, continuously monitoring your application for security vulnerabilities and performance issues. The AI-powered analysis engine learns your application's normal behavior patterns and alerts you to anomalies before they become critical problems.
For advanced configuration, custom alert rules, and enterprise features, check out our comprehensive documentation or contact our support team at support@securenow.com.