A simple HTTPS client with mutual TLS authentication using Node.js built-in modules.
- ✅ Mutual TLS authentication
- ✅ Server certificate verification
- ✅ Multiple endpoint tests
- ✅ No external dependencies
- ✅ Pretty printed JSON output
- Node.js 14+ (built-in
httpsmodule) - Certificates generated by the mtls CLI tool
- Running mTLS server (Node.js, Go, or Caddy)
# Install dependencies (no runtime dependencies needed)
npm install
# Or use yarn
yarn install# Using npm
npm start
# Or directly with node
node client.jsThe client tests 4 endpoints:
- GET / - Main endpoint, displays client certificate info
- GET /health - Health check endpoint
- GET /api/data - Fetches sample data with metadata
- POST /api/echo - Sends JSON and receives echo response
The client loads:
- Client certificate: Used to authenticate to the server
- Client private key: Used for TLS handshake
- CA certificate: Used to verify the server's certificate
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
ca: fs.readFileSync('ca-cert.pem'),
rejectUnauthorized: true // Verify server certificate
};You can modify the server URL in client.js:
const SERVER_URL = 'localhost';
const SERVER_PORT = 8443;🔒 mTLS Node.js Client
======================
📡 Test 1: Main endpoint (GET /)
✅ Status: 200
Message: mTLS Node.js Server
Client Certificate: localhost
Verified: true
Server Time: 2024-01-15T10:30:00.000Z
📡 Test 2: Health check (GET /health)
✅ Status: 200
Response: OK
📡 Test 3: API data (GET /api/data)
✅ Status: 200
Data:
{
"timestamp": "2024-01-15T10:30:00.000Z",
"client": { ... },
"server": { ... }
}
📡 Test 4: Echo test (POST /api/echo)
✅ Status: 200
Response: { ... }
✅ All tests completed successfully!
Connection refused:
- Make sure the server is running on the correct port
- Check if certificates are in the correct location
Certificate verification failed:
- Ensure all certificates are signed by the same CA
- Check certificate expiration dates
- Verify CA certificate is loaded correctly
EPROTO error:
- Server might not support the TLS version
- Certificate might be corrupted or in wrong format