Skip to content
Advertisement

Node.js https server doesn’t work when I separate it in two files

I have this node.js https server that works when it is in one app.js file but when I split it in 2 files it doesn’t work anymore. I don’t know why..

This app.js works

JavaScript

But when I separate it in 2 files app.js and certificate.js it doesn’t work anymore

app.js

JavaScript

certificate.js

JavaScript

I’m getting this syntax error

JavaScript

I also tried to to this

JavaScript

And I was getting this error

connection failed: Error in connection establishment: net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH

So when it was all in app.js file it worked perfectly. But when I tried to separate it in 2 files it doesn’t work anymore..

Advertisement

Answer

It’s a JS syntax error, it has nothing to do with your app being split in two files.

This object is invalid :

JavaScript

Try this :

JavaScript

EDIT : including @StephaneVanraes comments :

You are also importing the certificate wrong, try const { certificate } = require('./certificate.js')

Also, since the property names are the same in both cases you could use the spread operator here: https.createServer({ ...certificate })

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement