简体   繁体   中英

Nodejs Change server HTTP to HTTPS

I try change my server to HTTPS, but does not work fine. My page show like I created a new endpoint that returns anything. In this case, I have 2 files, app.js with express instantiate and routes, and my server.js to execute the server getting the instance from express.

My app.js (server-side):

const express = require('express'); // app server
const bodyParser = require('body-parser'); // parser for post requests
const http = require('http');
const https = require('https');
// instance of express
let app = express();

// endpoints here
// code .. code with routes, code...

module.exports = app;

My server.js (server-side):

const fs = require('fs')
const server = require('./app');
const port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;


const 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);
});

Maybe is a newbie question, I can have one file with everything on it, but I would like to be more modular.

Someone can help me please, or explain what I did wrong with this case use?

In your case, what you are exporting from app.js is an express app object. That's not the right kind of object to create an https server from. Instead, you need to manually create an https server and then associate your express app with that. You can see the Express doc for doing that here: https://expressjs.com/en/api.html#app.listen .

If you look at the code for app.listen() in the Express repository on Github , you will see that all it does is this:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

So, it is hard-wired to create an http server and has no way to create an https server.

To create an https server, you must create the server yourself and specify the app object as the request handler for it.

The general scheme for that is 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 you associate your Express object with that as the request handler. The app.listen() interface in Express does not offer https server creation so you have to do it yourself.


If you really want to use your two files, then you could 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM