Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js returning empty response for HTTPS

Tags:

node.js

I have the following extremely basic Node.js server:

"use strict";

const http = require("http");
const https = require("https");
const fs = require("fs");

http.createServer((req, res) => {
        console.log("regular works");
        res.end("Regular response");
}).listen(3000);

https.createServer({
        key: fs.readFileSync("/etc/letsencrypt/live/domain.com/privkey.pem"),
        cert: fs.readFileSync("/etc/letsencrypt/live/domain.com/cert.pem")
}, (req, res) => {
        console.log("secure works");
        res.end("Secure response");
}).listen(3001);

I run this as sudo node filename.js, only because files in /etc/letsencrypt/live are root-only. I will do this properly later, this is only for testing.

When run, I can hit port 3000 just fine. The server console prints regular works, and the browser displays Regular response. However, port 3001 returns an empty response, and no message is printed to the server.

The LetsEncrypt files were generated with ./letsencrypt-auto certonly --standalone -d domain.com --email [email protected] --agree-tos and appear valid.

What am I missing to have the expected result?

like image 853
Scott Avatar asked Sep 10 '26 05:09

Scott


2 Answers

There are two issues here:

  • Assuming you're not obscuring the real hostname/IP, you should use 127.0.0.1 or similar (if you're on the same machine) instead of 255.255.255.255.

  • HTTP is the default for cURL, so you're currently sending a plaintext HTTP request to your HTTPS server, which is not going to work (the HTTPS server sees the literal HTTP request as an invalid TLS handshake which causes the connection to end abruptly). To remedy this, explicitly include https:// (e.g. curl -I --verbose https://127.0.0.1:3001).

like image 119
mscdex Avatar answered Sep 11 '26 18:09

mscdex


need to check that the URL contains https:// not http://

like image 20
Shimon Doodkin Avatar answered Sep 11 '26 18:09

Shimon Doodkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!