在Node.js中进行REST调用,需要客户端证书进行身份验证

Ana*_*ran 10 rest certificate node.js

有没有办法进行休息调用,需要通过Node.js进行身份验证的客户端证书?

Joa*_*son 17

是的,你可以很简单地做到这一点,这里使用常规的https请求完成;

var https = require('https'),                  // Module for https
    fs =    require('fs');                     // Required to read certs and keys

var options = {
    key:   fs.readFileSync('ssl/client.key'),  // Secret client key
    cert:  fs.readFileSync('ssl/client.crt'),  // Public client key
    // rejectUnauthorized: false,              // Used for self signed server
    host: "rest.localhost",                    // Server hostname
    port: 8443                                 // Server port
};

callback = function(response) {
  var str = '';    
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

https.request(options, callback).end();
Run Code Online (Sandbox Code Playgroud)