12

I am trying to start a https node.js server.

I started by creating a certificate and key following this guide:

http://gaboesquivel.com/blog/2014/nodejs-https-and-ssl-certificate-for-development/

and I placed them in my /app_name/security/keys directory.

To start my https server, I have the following:

const https         = require('https'),
      fs            = require('fs');

if(app.get('env') === 'development') {

    console.log('dev env!!'); //prints correctly
    console.log('port: ' + port); //prints correctly
    const options = {
        key: fs.readFileSync('./security/keys/key.pem'),
        cert: fs.readFileSync('./security/keys/cert.pem')
    };   

    https.createServer(options, (req, res) => {

        console.log('https good to go'); //this does not print out anything

    }).listen(port);

}

When I go to https://localhost:3000, the page throws an error

This site can’t be reached

localhost unexpectedly closed the connection.
ERR_CONNECTION_CLOSED

But there's no error on the server side console. Furthermore, if i go to the regular localhost:3000, I get:

The localhost page isn’t working

localhost didn’t send any data.
ERR_EMPTY_RESPONSE

Can someone help?

Thanks in advance!

---- UPDATE ----

I'm running on port 443 now. Initially I got an error:

Error: listen EACCES 0.0.0.0:443 so I ran:

sudo NODE_ENV=development nodemon app

Which did not throw any errors. However, when I went to https://localhost:443, I get:

This site can’t be reached

localhost unexpectedly closed the connection.
Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • I guess it could be related with the port, https usualy works on 443 port. you can try. – Alexandru Olaru Mar 31 '16 at 14:56
  • if no port is given then it works at 443. there is port constant defined. – tanaydin Mar 31 '16 at 14:59
  • Gotcha - I tried running on 443 but I'm still getting `this site can't be reached`. I updated my question with more error details. – Trung Tran Mar 31 '16 at 15:04
  • Regarding ports, it doesn't really matter should work on 3000. When you are generating keys what are you setting as a FQDN? Also please provide information about your system. – Dauren Akilbekov Mar 31 '16 at 15:50
  • Hmm.. i'm probably not. The only thing I specify that is specific to my server information is the common name is `localhost`.. Could you provide a link on how to properly set it up as a FQDN? – Trung Tran Mar 31 '16 at 18:18
  • Possible duplicate of [How to create an HTTPS server in Node.js?](http://stackoverflow.com/questions/5998694/how-to-create-an-https-server-in-node-js) – Felipe Sabino Sep 20 '16 at 00:00
  • given that you have not defined the variable `port` with `var port` it will through an error, I would suggest you use devtool to debug nodejs https://github.com/Jam3/devtool, it will probably tell you what the problem is in more detail. just like in the browser. – Val Oct 04 '16 at 16:46
  • try ```https.createServer(options, app).listen(portssl, () => console.log('SSL port ' + portssl) );``` – Maicon Santana Aug 28 '19 at 15:11

2 Answers2

2

I used express as a web server. to install express:

npm install express --save

I took your code, added the usage in express, generated certificate using openssl, and executed it - all looked good, the server was up, listening to port 3000 over https.

My code (which is based on your code...):

var app = require('express')();
const https         = require('https'),
      fs            = require('fs'),
      port          = 3000;

if(app.get('env') === 'development') {

    console.log('dev env!!'); //prints correctly
    console.log('port: ' + port); //prints correctly
    const options = {
        key: fs.readFileSync('/tmp/private.key'),
        cert: fs.readFileSync('/tmp/publickey.crt')
    };

    https.createServer(options, (req, res) => {

        console.log('https good to go'); //this does message appears!!! ^_^

    }).listen(port);

}

Please pay attention to the way I defined app: var app = require('express')();

You can split this definition into two line if it's more readable:

var express = require('express');
var app = express();
yaniv israel
  • 166
  • 1
  • 8
-1

So many problems with your code.

I tested this really quickly.

the keyword app and port is not defined, lines 4 and 7 respectively.

That will throw you a syntax error, preventing the code from continuing any further therefore server not starting up at all.

As I mentioned on my comment, use devtool to debug and use the following line on a CLI devtool server.js -w where the -w watches for file changes and reloads the server on the fly, while developing. also assuming you named your entry file server.js

Val
  • 17,336
  • 23
  • 95
  • 144