2015-11-12 11 views
5

Sto usando node.js restify ver4.0.3Get restify server di REST API per supportare sia HTTPS e HTTP

Il semplice codice seguente lavora come un semplice server API REST che supporta HTTP. Una chiamata esempio API è http://127.0.0.1:9898/echo/message

var restify = require('restify'); 

var server = restify.createServer({ 
    name: 'myapp', 
    version: '1.0.0' 
}); 
server.use(restify.acceptParser(server.acceptable)); 
server.use(restify.queryParser()); 
server.use(restify.bodyParser()); 

//http://127.0.0.1:9898/echo/sdasd 
server.get('/echo/:name', function (req, res, next) { 
    res.send(req.params); 
    return next(); 
}); 

server.listen(9898, function() { 
    console.log('%s listening at %s', server.name, server.url); 
}); 

Supponiamo che voglio supportare HTTPS ed effettuare la chiamata API https://127.0.0.1:9898/echo/message

Come si può fare?

Ho notato che il ripristino delle modifiche al codice è piuttosto veloce e il codice precedente con la versione precedente potrebbe non funzionare con la versione più recente.

+1

Hai controllato http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/? –

+0

Grazie. Sembra buono. Sto provando un esempio basato su quel link. Alcuni problemi al momento. – user781486

risposta

9

Per usare HTTPS, è necessario una chiave e un certificato:

var https_options = { 
    key: fs.readFileSync('/etc/ssl/self-signed/server.key'), 
    certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt') 
}; 
var https_server = restify.createServer(https_options); 

dovrai iniziare a entrambi i server per consentire HTTP e HTTPS accesso:

http_server.listen(80, function() { 
    console.log('%s listening at %s', http_server.name, http_server.url); 
});. 
https_server.listen(443, function() { 
    console.log('%s listening at %s', https_server.name, https_server.url); 
});. 

Per configurare instradare al server, dichiarare le stesse route per entrambi i server, reindirizzamento tra HTTP e HTTPS in base alle esigenze:

http_server.get('/1', function (req, res, next) { 
    res.redirect('https://www.foo.com/1', next); 
}); 
https_server.get('/1', function (req, res, next) { 
    // Process the request 
}); 

Quanto sopra ascolta le richieste su un percorso /1 e semplicemente lo reindirizza al server HTTPS che lo elabora.

9

Grazie al commento di Bas van Stein, ecco un esempio operativo completo.

var restify = require('restify'); 
    var fs = require('fs'); 

    // Setup some https server options 
    //generated from http://www.selfsignedcertificate.com/ 
    var https_options = { 
     key: fs.readFileSync('./HTTPS.key'), //on current folder 
     certificate: fs.readFileSync('./HTTPS.cert') 
    }; 

    // Instantiate our two servers 
    var server = restify.createServer(); 
    var https_server = restify.createServer(https_options); 

    // Put any routing, response, etc. logic here. This allows us to define these functions 
    // only once, and it will be re-used on both the HTTP and HTTPs servers 
    var setup_server = function(app) { 
     function respond(req, res, next) { 
      res.send('I see you ' + req.params.name); 
     } 

     // Routes 
     app.get('/test/:name', respond); 
    } 

    // Now, setup both servers in one step 
    setup_server(server); 
    setup_server(https_server); 

    // Start our servers to listen on the appropriate ports 
    server.listen(9848, function() { 
     console.log('%s listening at %s', server.name, server.url); 
    }); 

    https_server.listen(443, function() { 
     console.log('%s listening at %s', https_server.name, https_server.url); 
    });