In your case, you are exporting an express app object from app.js This is not the correct type of object to create an https server. Instead, you need to manually create an https server and then associate your express application with it. You can see the express document for this: https://expressjs.com/en/api.html#app.listen .
If you look at the app.listen() code in the Express repository in Github , you will see that all it does is:
app.listen = function listen() { var server = http.createServer(this); return server.listen.apply(server, arguments); };
Thus, it is difficult to connect to create an http server and is not able to create an https server.
To create an https server, you must create the server yourself and specify the app object for it as the request handler.
The general scheme for this:
var express = require('express'); var https = require('https'); var app = express(); var options = {...};
Note that you manually use the https module to create the https server object, and then associate your Express object with this as a request handler. The app.listen() interface in Express does not offer the creation of an https server, so you need to do it yourself.
If you really want to use your two files, you can do this:
app.js
var express = require('express'); // app server var bodyParser = require('body-parser'); // parser for post requests //all endpoints is inside my app.js var app = express(); // endpoints here //code .. code with routes, code... module.exports = app;
server.js
var fs = require('fs') var app = require('./app'); var port = process.env.PORT || process.env.VCAP_APP_PORT || 443; var https = require('https'); var options = { key: fs.readFileSync('certificates/xxx.key'), cert: fs.readFileSync('certificates/xxx.cer') }; https.createServer(options, app).listen(port);