Scott
Scott

Reputation: 5379

Node.js returning empty response for HTTPS

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?

Upvotes: 3

Views: 6249

Answers (2)

Shimon Doodkin
Shimon Doodkin

Reputation: 4579

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

Upvotes: 4

mscdex
mscdex

Reputation: 106736

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).

Upvotes: 6

Related Questions