Nodejs Change HTTP Server to HTTPS

I am trying to change my server to HTTPS but it is not working fine. My page shows that I am creating a new server with anything. In this case, I have 2 files, app.js with express and routes, and server.js requires ./app to execute server.

My app.js (server side):

 var express = require('express'); // app server var bodyParser = require('body-parser'); // parser for post requests var http = require('http'); var https = require('https'); //all endpoints is inside my app.js var app = express(); // endpoints here //code .. code with routes, code... module.exports = app; 

My server.js (server side):

  var fs = require('fs') var server = require('./app'); var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000; var options = { key: fs.readFileSync('certificates/xxx.key'), cert: fs.readFileSync('certificates/xxx.cer') }; server.listen(port, options, function() { console.log('Server execute here %d', port); }); 

Perhaps this is a question for beginners, I can create one server with only one file, but I do not know how to create it ...

Can someone help me and explain what I did wrong with this case?

+5
source share
1 answer

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 = {...}; // read certificates in here https.createServer(options, app).listen(443); 

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); 
+3
source

Source: https://habr.com/ru/post/1266989/


All Articles