Petro Ivanenko
Petro Ivanenko

Reputation: 727

Simple https express results in "This page isn't working"

I have a very simple node setup with only express installed. I pasted this command from here into my Ubuntu 20 bash

openssl req -nodes -new -x509 -keyout server.key -out server.cert

When I run this code:

const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const httpPort = 3000;
const httpsPort = 3001;
app = express();

var key = fs.readFileSync(__dirname + '/server.key');
var cert = fs.readFileSync(__dirname + '/server.cert');

var credentials = {
  key: key,
  cert: cert,
};

//GET home route
app.get('/', (req, res) => {
  res.send('Hello World.');
});

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(httpPort, () => {
  console.log('Http server listing on port : ' + httpPort);
});

httpsServer.listen(httpsPort, () => {
  console.log('Https server listing on port : ' + httpsPort);
});

This results in an error when I access localhost:3001

enter image description here

What am I doing wrong?

Thanks in advance for your time!

Upvotes: 0

Views: 311

Answers (1)

Petro Ivanenko
Petro Ivanenko

Reputation: 727

My mistake was that I typed localhost:3001 in my browser search which made me request http://localhost:3001.

Typing https://localhost:3001 worked.

Upvotes: 1

Related Questions