सभी डिप्लॉयमेंट गाइड डिप्लॉयमेंट

Node.js के साथ SSL Certificate कैसे Use करें

Node.js में https module के through built-in HTTPS support है। आप अपनी certificate files load करें और एक HTTPS server बनाएं। यह guide raw Node.js, Express, और recommended production setup को cover करता है।

Basic HTTPS Server

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('/etc/ssl/privkey.pem'),
  cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('Hello HTTPS');
}).listen(443);

Express के साथ HTTPS

const https = require('https');
const fs = require('fs');
const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello HTTPS');
});

const options = {
  key: fs.readFileSync('/etc/ssl/privkey.pem'),
  cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
};

https.createServer(options, app).listen(443, () => {
  console.log('HTTPS server running on port 443');
});

HTTP से HTTPS पर Redirect करें

HTTP और HTTPS दोनों servers run करें:

const http = require('http');
const https = require('https');
const fs = require('fs');
const express = require('express');

const app = express();
// ... your routes

const options = {
  key: fs.readFileSync('/etc/ssl/privkey.pem'),
  cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
};

// HTTPS server
https.createServer(options, app).listen(443);

// HTTP redirect
http.createServer((req, res) => {
  res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` });
  res.end();
}).listen(80);

Production Recommendation: Reverse Proxy

Production के लिए, Node.js में directly TLS terminate न करें। सामने एक reverse proxy (Nginx, Caddy, या load balancer) use करें:

Client ──HTTPS──→ Nginx (TLS termination) ──HTTP──→ Node.js (port 3000)

Reasons:

  • Nginx TLS को ज्यादा efficiently handle करता है (C vs JavaScript)
  • Certificate renewal के लिए Node.js restart करने की need नहीं
  • Port 443 के लिए root required है — Node.js को root के रूप में नहीं चलाना चाहिए
  • Rate limiting, caching, compression proxy handle करता है
  • HTTP/2 support Nginx में ज्यादा mature है

Node.js direct HTTPS development, internal services, या छोटे projects के लिए fine है।

Common Frameworks

Fastify

const fastify = require('fastify')({
  https: {
    key: fs.readFileSync('/etc/ssl/privkey.pem'),
    cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
  },
});

fastify.get('/', async () => ({ hello: 'https' }));
fastify.listen({ port: 443, host: '0.0.0.0' });

Koa

const Koa = require('koa');
const https = require('https');
const fs = require('fs');

const app = new Koa();
app.use(ctx => { ctx.body = 'Hello HTTPS'; });

https.createServer({
  key: fs.readFileSync('/etc/ssl/privkey.pem'),
  cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
}, app.callback()).listen(443);

Next.js / Nuxt / Other Framework Dev Servers

Framework dev servers में usually local development के लिए --https flag होता है। Production के लिए, हमेशा reverse proxy use करें — ये frameworks Nginx, Caddy, या cloud load balancer के behind static files serve करते हैं या SSR run करते हैं।

Environment Variable Pattern

Certificate paths hardcode न करें। Environment variables use करें ताकि same code dev और production में काम करे:

const options = process.env.SSL_KEY ? {
  key: fs.readFileSync(process.env.SSL_KEY),
  cert: fs.readFileSync(process.env.SSL_CERT),
} : null;

if (options) {
  https.createServer(options, app).listen(443);
  console.log('HTTPS server on port 443');
} else {
  app.listen(3000);
  console.log('HTTP server on port 3000 (no SSL_KEY set)');
}

Self-signed Certificates के साथ Development

Local development के लिए, एक self-signed certificate बनाएं (production के लिए नहीं):

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
  -keyout dev-key.pem -out dev-cert.pem -days 365 -nodes \
  -subj "/CN=localhost"
const options = {
  key: fs.readFileSync('./dev-key.pem'),
  cert: fs.readFileSync('./dev-cert.pem'),
};

आपका browser एक warning दिखाएगा (self-signed certificates trusted नहीं होते) — development के लिए click करके आगे बढ़ें।

Certificates का Hot-Reloading

बिना restart किए certificate reload करने के लिए (Let’s Encrypt renewal के लिए useful):

const tls = require('tls');

function loadCerts() {
  return {
    key: fs.readFileSync('/etc/ssl/privkey.pem'),
    cert: fs.readFileSync('/etc/ssl/fullchain.pem'),
  };
}

const server = https.createServer({
  SNICallback: (hostname, cb) => {
    const ctx = tls.createSecureContext(loadCerts());
    cb(null, ctx);
  },
}, app);

यह हर नए connection पर files फिर से read करता है। Better performance के लिए, एक file watcher add करें जो सिर्फ files change होने पर reload करे।

FAQ

क्या मुझे Node.js में SSL handle करना चाहिए या reverse proxy use करना चाहिए?

Production के लिए: reverse proxy use करें। Development, छोटे projects, या internal services के लिए: Node.js direct HTTPS fine है। Reverse proxy pattern standard है क्योंकि यह concerns separate करता है और TLS को ज्यादा efficiently handle करता है।

क्या मैं Node.js के साथ GetHTTPS certificates use कर सकता हूं?

हां। GetHTTPS standard PEM files (privkey.pem, fullchain.pem) produce करता है जिन्हें Node.js fs.readFileSync() से directly read करता है।

Node.js में certificate renewal कैसे handle करें?

अगर reverse proxy use कर रहे हैं, तो बस files replace करें और proxy reload करें — Node.js restart करने की need नहीं। अगर TLS directly handle कर रहे हैं, तो ऊपर दिए गए SNICallback approach use करें या certificate files replace करने के बाद Node.js process restart करें।

Development के लिए localhost SSL के बारे में क्या?

Self-signed certificate (ऊपर दिखाया गया) या locally trusted development certificate के लिए mkcert use करें। Localhost के लिए Let’s Encrypt use न करें — यह ऐसे domain को validate नहीं कर सकता जो publicly accessible नहीं है।

क्या मैं Deno या Bun के साथ GetHTTPS certificates use कर सकता हूं?

हां। Deno और Bun दोनों PEM files के साथ TLS support करते हैं:

Deno:

Deno.serve({
  port: 443,
  cert: Deno.readTextFileSync("/etc/ssl/fullchain.pem"),
  key: Deno.readTextFileSync("/etc/ssl/privkey.pem"),
}, (req) => new Response("Hello HTTPS"));

Bun:

Bun.serve({
  port: 443,
  tls: {
    cert: Bun.file("/etc/ssl/fullchain.pem"),
    key: Bun.file("/etc/ssl/privkey.pem"),
  },
  fetch(req) { return new Response("Hello HTTPS"); },
});

GetHTTPS की same PEM files सभी JavaScript runtimes में काम करती हैं।

संबंधित लेख

शुरुआत करें 2026-05-08
Free SSL certificate कैसे पाएं (step-by-step guide)
Let's Encrypt से 5 minutes में free SSL certificate पाएं — कोई software install नहीं, कोई account नहीं बनाना। 4 methods, दोनों challenge types, 6 platforms पर installation, और troubleshooting cover करने वाली complete guide।
डिप्लॉयमेंट 2026-05-08
Docker और Reverse Proxy के साथ SSL Certificate
Nginx reverse proxy, automatic Let's Encrypt के साथ Traefik, या manual certificate mounting use करके Docker containers के लिए HTTPS configure करें।
SSL और Certificates 2026-05-07
SSL Certificate Formats: PEM, PFX, DER explained
PEM, PFX/PKCS#12, और DER certificate formats को समझें। जानें आपके server को कौन सा format चाहिए और OpenSSL से उनके बीच कैसे convert करें।
अपने browser में मुफ़्त SSL certificate पाएँ
कोई installation नहीं, कोई account नहीं। आपकी private key कभी आपका device नहीं छोड़ती।
अपना certificate पाएँ