[READ-ONLY] Mirror of https://github.com/usrrname/winserver. Boilerplate node app made for attempting deployment on Windows VM
PowerShell 70%
TypeScript 23%
JavaScript 7%
<1%

README.md

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: #

  1. Node.js (LTS version recommended)

  2. PM2 (Process Manager) - Optional but recommended

    npm install -g pm2
    npm install -g pm2-windows-service
    

Local Development #

  1. Install dependencies:

    npm install
    
  2. Run in development mode:

    npm run dev
    
  3. Build the project:

    npm run build
    
  4. Run the production build:

    npm start
    

Deployment on Windows Server 2019 #

Option 1: Manual Deployment #

  1. Copy project files to the server (exclude node_modules and dist)

  2. Install dependencies:

    npm install --production
    
  3. Build the application:

    npm run build
    
  4. Start the server:

    npm start
    

PM2 provides automatic restarts, monitoring, and log management.

  1. Install PM2 globally:

    npm install -g pm2
    
  2. Start the application:

    pm2 start ecosystem.config.js
    
  3. Configure PM2 as a Windows Service:

    npm install -g pm2-windows-service
    pm2-service-install
    
  4. 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 #

  1. Install node-windows:

    npm install -g node-windows
    
  2. 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()
    
  3. 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 PORT environment 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:

  1. Right-click "This PC" → Properties
  2. Advanced system settings → Environment Variables
  3. 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 #

  1. Use PM2 or Windows Service for automatic restarts
  2. Configure logging and log rotation
  3. Set up monitoring and alerting
  4. Use environment variables for configuration
  5. Enable HTTPS with a reverse proxy (IIS or nginx)
  6. Implement rate limiting for production use
  7. 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 #

  1. Stop the application:

    pm2 stop winserver
    
  2. Pull latest changes or update files

  3. Rebuild:

    npm run build
    
  4. Restart:

    pm2 restart winserver
    

License #

ISC