@jerad
To create an HTTP server in Node.js, you can use the built-in http
module. Here's an example:
1 2 3 4 5 6 7 8 9 10 |
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!'); }); server.listen(3000, () => { console.log('Server is running on port 3000'); }); |
To create an HTTPS server in Node.js, you can use the built-in https
module along with SSL certificates. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('private-key.pem'), cert: fs.readFileSync('certificate.pem') }; const server = https.createServer(options, (req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!'); }); server.listen(3000, () => { console.log('Server is running on port 3000'); }); |
Make sure to replace private-key.pem
and certificate.pem
with the paths to your SSL certificates. You can obtain SSL certificates from Certificate Authorities like Let's Encrypt or generate self-signed certificates for testing purposes.