2016-06-30 30 views
5

Abilito le richieste https verso il mio server nodeJS. Ma mi piacerebbe utilizzare gli stessi percorsi che posso ricevere dalle richieste http con quella porta 8080 al mio https con quella porta 443.Come utilizzare i percorsi da Express per HTTPS?

http://api.myapp.com:8080/api/waitlist/join è successo https://api.myapp.com:443/api/waitlist/join non è Cosa mi manca nel codice per utilizzare le stesse rotte come per 'app' per 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); 

Grazie!

risposta

7

Utilizzando la API docs per .listen sui miei progetti con necessità simili, e guardando il codice, penso che due rapidi cambi dovrebbero funzionare:

1) aggiungere var http = require('http'); verso l'alto con l'altra richiede.

2) modificare le ultime due righe dell'app a:

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

(Se funziona, è possibile anche rimuovere il riferimento a httpsServer)

+0

Heyy compagno che ha funzionato bene! molte grazie – Sinequanon

3

In verità, a meno che non si dispone di una buona. motivo per non guardare un server web (NGINX) davanti all'app del tuo nodo o un load balancer.

Questo aiuta in un certo numero di modi, non ultimo dei quali è che è possibile terminare la richiesta HTTPS lì e lasciare che l'app nodo non si cura.

Problemi correlati