You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何安全公开Windows Server端口,远程访问Node.js API

Hey there! Let's walk through getting your Node.js API up and running publicly on your Windows Server with PM2, while making sure it's locked down tight. Here's what you need to do:

1. Configure Your API to Listen on All Network Interfaces

Right now, your API is only listening to localhost, which means it won't accept requests from outside the server itself. Update your listen code to bind to 0.0.0.0 instead—this lets it accept traffic from any network interface on the server:

// Example for Express.js
app.listen(3131, '0.0.0.0', () => {
  console.log('API is running and ready for external requests on port 3131');
});
2. Set Up PM2 to Keep Your API Running 24/7

PM2 is perfect for keeping your API online even after server restarts. Here's how to set it up on Windows:

  • First, install PM2 globally if you haven't already: npm install pm2 -g
  • Navigate to your API's directory, then start it with PM2: pm2 start your-api-file.js (replace your-api-file.js with your actual entry file)
  • Save the PM2 process list so it persists after reboots: pm2 save
  • Set PM2 to start automatically when Windows boots: pm2 startup windows (follow the on-screen prompts to complete the setup)
3. Open Port 3131 in Windows Firewall

Windows Firewall will block incoming requests to port 3131 by default—you need to create an inbound rule to allow this traffic:

  • Open Windows Defender Firewall with Advanced Security
  • Go to Inbound Rules > New Rule
  • Select Port > Next > Choose TCP and enter 3131 in the "Specific local ports" field
  • Select Allow the connection > Next > Check the network profiles that apply (make sure Public is checked if you want external access)
  • Name the rule something like "Node.js API - Port 3131" and finish the setup
4. Make Your API Accessible Remotely

How you do this depends on whether your server has a public IP or is behind a router:

  • If your server has a public static IP:
    You can now access your API directly using http://your-public-ip-address:3131 (we'll switch this to HTTPS for security next).
  • If your server is on a local network (behind a router):
    You need to set up port forwarding on your router:
    1. Log into your router's admin panel (usually via http://192.168.1.1 or similar—check your router's docs)
    2. Look for "Port Forwarding" or "Virtual Server" settings
    3. Create a new rule: forward external port 3131 (you can use a different external port if you want, like 8080, but keep the internal port as 3131) to your server's local IP address (find this via ipconfig on the server)
    4. Save the rule—now external traffic to your router's public IP on that port will be sent to your API
5. Critical Security Steps (Don't Skip These!)

Publicly exposing an API without security is risky—here's how to lock it down:

  • Enable HTTPS: This encrypts all traffic between clients and your API, preventing data snooping. Use Let's Encrypt to get a free SSL certificate (you can install Certbot on Windows to automate this). You can either configure HTTPS directly in Node.js:
    const https = require('https');
    const fs = require('fs');
    const express = require('express');
    const app = express();
    
    const sslOptions = {
      key: fs.readFileSync('path/to/your/private-key.pem'),
      cert: fs.readFileSync('path/to/your/certificate.pem')
    };
    
    https.createServer(sslOptions, app).listen(3131, '0.0.0.0', () => {
      console.log('HTTPS API running on port 3131');
    });
    
    Or use Nginx as a reverse proxy to handle HTTPS (this is often easier to manage long-term, as Nginx can handle certificate renewals and load balancing if needed).
  • Restrict Access Where Possible: If only specific IPs need to use your API, update the Windows Firewall inbound rule to only allow those IPs instead of all public traffic.
  • Add API Authentication: Implement JWT tokens, API keys, or OAuth2 to ensure only authorized clients can call your endpoints. For example, use the express-jwt middleware for Express.js to validate tokens on every request.
  • Hide Server Fingerprints: Disable the default X-Powered-By header in Express to avoid giving attackers info about your tech stack: app.disable('x-powered-by');
  • Secure Your MSSQL Connection: Make sure your MSSQL Server only allows connections from your Node.js API's server IP (don't expose MSSQL's default port 1433 to the public). Update MSSQL's firewall rules and SQL Server login settings to restrict access.
  • Keep Everything Updated: Regularly update Node.js, PM2, your npm dependencies, and Windows Server itself to patch security vulnerabilities.
6. Test Remote Access

Once everything is set up, test from a device not on your local network (like a phone using cellular data) by visiting https://your-public-ip:3131/your-api-endpoint—if it returns the expected data, you're good to go! If not, double-check your firewall rules, port forwarding settings, and API listen address.

内容的提问来源于stack exchange,提问作者Raja Osama

火山引擎 最新活动