0

I have create an API using Express in Node JS, and before, I use http const server = http.createServer(app); and it worked fine, no issue at all.

But I tried using https because to make it more secure. I've defined the options with paths to my certificate and secret key

const options = {
  key: fs.readFileSync('./key.key'),
  cert: fs.readFileSync('./cert.crt')
};

const server = https.createServer(options, app);

But when I tried to send a request, it keeps loading, and eventually after come minutes, it gives me request timeout. This is the screenshot of the loading when I send a request.

Can someone please help me? Oh, when I visit the page with my browser, this is what it returned after the long pending. Notes that the page is already using the ssl certificate.

I have tried modify the certificate, assuming that it doesn't work because my certificate is not valid, but it returns an error instead of giving me request timeout. So, I my certificate and my secret key is not the issue here.

Valda
  • 1
  • 1
  • Are the files you're providing in your options definitely correct? `key` should be a private key in PEM format, and not encrypted (unless you want to use the `passphrase` option), and `cert` should be the certificate chain for that key. And when your server listens, are you passing a callback with a log, or do you have any other kind of logging going on that shows that it's actually up and running? And, do you have anything in front of the app that was previously expecting to hit your app over HTTP (and possibly on a different port)? – Zac Anger May 06 '23 at 06:13
  • @ZacAnger The files I provide is definitely correct, I've checked it many times, and ensuring the path is correct. I'm gonna try converting the key to PEM, do I also need to convert the cert? My server also has a callback when it listens, but like I said, its only keep loading and doesn't log anything. And I don't think there is anything in front of the app, but as I use express, there are only some express routing. – Valda May 06 '23 at 06:26
  • If the cert is already a [PEM file with the certificate chain for that key](https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions), then it should be fine, if not then it does need to be in the right format. You can try checking `server.listen` (example: `console.log("Server is listening?", server.listening)`) after your `server.listen()` call to see if it's actually up and running. If your cert is self-signed then you could also try setting `NODE_TLS_REJECT_UNAUTHORIZED=0` (as an env var, example: `NODE_TLS_REJECT_UNAUTHORIZED=0 node my-server.js`). – Zac Anger May 06 '23 at 06:43
  • @ZacAnger I converted my cert and key by [following these steps](https://stackoverflow.com/questions/991758/how-to-get-pem-file-from-key-and-crt-files), but the issue still persist. Also, already tried your suggestion but it seems that my server doesn't listen. And I need to point this up, but I got the files from my hosting platform. – Valda May 06 '23 at 06:57
  • Did you change `http` to `https` in the url? – Konrad May 06 '23 at 07:10
  • @Konrad already did, still the same. – Valda May 06 '23 at 07:11
  • @ZacAnger I apologize, it turns out my server is actually listening. But the issue still the same, when I send a request, it doesn't give any response. – Valda May 06 '23 at 11:36

0 Answers0