Windows Server Node.js Application #
A simple Node.js TypeScript server that returns "hello world", configured for deployment on Windows Server 2019 Data Center.
Features #
- Built with Express.js and TypeScript
- Health check endpoint
- Graceful shutdown handling
- PM2 process manager configuration
- Production-ready setup for Windows Server
Prerequisites #
On Windows Server 2019 VM instance on GCP: #
-
Node.js (LTS version recommended)
- Download from: https://nodejs.org/
-
PM2 (Process Manager) - Optional but recommended
npm install -g pm2 npm install -g pm2-windows-service
Local Development #
-
Install dependencies:
npm install -
Run in development mode:
npm run dev -
Build the project:
npm run build -
Run the production build:
npm start
Deployment on Windows Server 2019 #
Option 1: Manual Deployment #
-
Copy project files to the server (exclude node_modules and dist)
-
Install dependencies:
npm install --production -
Build the application:
npm run build -
Start the server:
npm start
Option 2: PM2 Process Manager (Recommended) #
PM2 provides automatic restarts, monitoring, and log management.
-
Install PM2 globally:
npm install -g pm2 -
Start the application:
pm2 start ecosystem.config.js -
Configure PM2 as a Windows Service:
npm install -g pm2-windows-service pm2-service-install -
PM2 Commands:
pm2 list # List all processes pm2 logs # View logs pm2 monit # Monitor processes pm2 restart winserver # Restart the app pm2 stop winserver # Stop the app pm2 delete winserver # Remove from PM2
Option 3: Windows Service with node-windows #
-
Install node-windows:
npm install -g node-windows -
Create a service installation script (service-install.js):
var Service = require("node-windows").Service var svc = new Service({ name: "WinServer Node App", description: "Node.js TypeScript Hello World Server", script: "C:\\path\\to\\winserver\\dist\\server.js", nodeOptions: ["--max_old_space_size=4096"], }) svc.on("install", function () { svc.start() }) svc.install() -
Run the installation script:
node service-install.js
Configuration #
Environment Variables #
Create a .env file in the root directory (optional):
PORT=3000
NODE_ENV=production
Port Configuration #
The server uses port 3000 by default. To change it:
- Set the
PORTenvironment variable - Or modify the port in
src/server.ts
Firewall Configuration #
Open the port in Windows Firewall:
netsh advfirewall firewall add rule name="Node Server Port 3000" dir=in action=allow protocol=TCP localport=3000
API Endpoints #
GET /- Returns "hello world"GET /health- Health check endpoint (returns JSON status)
Testing #
After deployment, test the endpoints:
curl http://localhost:3000
curl http://localhost:3000/health
Or open in a browser:
Monitoring #
Logs Location #
When using PM2:
- Log files are stored in
./logs/directory - Error logs:
./logs/err.log - Output logs:
./logs/out.log - Combined logs:
./logs/combined.log
Performance Monitoring #
pm2 monit # Real-time monitoring
pm2 info winserver # Detailed process info
Troubleshooting #
Port Already in Use #
# Find process using the port
netstat -ano | findstr :3000
# Kill the process (replace PID with actual process ID)
taskkill /PID <PID> /F
Node.js Not Found #
Ensure Node.js is added to the system PATH:
- Right-click "This PC" → Properties
- Advanced system settings → Environment Variables
- Add Node.js installation path to PATH variable
Permission Issues #
Run Command Prompt or PowerShell as Administrator when:
- Installing global packages
- Creating Windows services
- Modifying firewall rules
Production Best Practices #
- Use PM2 or Windows Service for automatic restarts
- Configure logging and log rotation
- Set up monitoring and alerting
- Use environment variables for configuration
- Enable HTTPS with a reverse proxy (IIS or nginx)
- Implement rate limiting for production use
- Set up regular backups of application files
Security Considerations #
- Keep Node.js and dependencies updated
- Use a reverse proxy (IIS) for SSL/TLS termination
- Implement authentication if needed
- Use environment variables for sensitive data
- Configure Windows Firewall appropriately
- Run the service with minimal required permissions
Updating the Application #
-
Stop the application:
pm2 stop winserver -
Pull latest changes or update files
-
Rebuild:
npm run build -
Restart:
pm2 restart winserver
License #
ISC