简体   繁体   中英

How to use routes from Express for HTTPS?

I'm enabling https requests toward my nodeJS server. But I would like to use the same routes that I can receive from http requests with that 8080 port to my https with that 443 port.

http://api.myapp.com:8080/api/waitlist/join is successful https://api.myapp.com:443/api/waitlist/join isn't What do I miss in the code to use the same routes as for 'app' for httpsServer?

var fs              = require('fs');
var https           = require('https');
var express         = require('express');       // call express
var app             = express();                // define our app using express
var bodyParser      = require('body-parser');
var mongoose        = require('mongoose');
var cors            = require('cors');
var config          = require('./config');

// Configure app to use bodyParser()
[...]
// Configure the CORS rights
app.use(cors());

// Enable https 
var privateKey = fs.readFileSync('key.pem', 'utf8');
var certificate = fs.readFileSync('cert.pem', 'utf8');

var credentials = {
    key: privateKey,
    cert: certificate
};

var httpsServer = https.createServer(credentials, app);

// Configure app port
var port            = process.env.PORT || config.app.port; // 8080

// Configure database connection
[...]

// ROUTES FOR OUR API
// =============================================================================

// Create our router
var router = express.Router();

// Middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('>>>> Something is happening. Here is the path: '+req.path);
    next();
});

// WAITLIST ROUTES ---------------------------------------
// (POST) Create Email Account --> Join the waitList
router.route('/waitlist/join').post(waitlistCtrl.joinWaitlist);
// And a lot of routes...


// REGISTER OUR ROUTES -------------------------------
// All of our routes will be prefixed with /api
app.use('/api', router);



// START THE SERVER
// =============================================================================
app.listen(port);
httpsServer.listen(443);

Thanks!

Using the API docs for .listen on my own projects with similar need, and looking at your code, I think two quick changes should work:

1) add var http = require('http'); to the top with your other requires.

2) change the last two lines of the app to:

// START THE SERVER
// =============================================================================
http.createServer(app).listen(port);
https.createServer(credentials, app).listen(443);

(If this works, you can also remove the reference to httpsServer .)

Truthfully, unless you have a very good reason not to I'd look at putting either a web server (NGINX) in front of your node app or a load balancer.

This helps in a number of ways, not the least of which being that you can terminate the HTTPS request there and let your node app not care.

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