我在我的node.js https服务器中使用.pfxauthentication,我不能在选项中使用“密码”

我目前正在使用node.js和https进行Web应用程序。 所以我尝试使用我的.pfx(我从http://www.cert-depot.com/获得了这个文件)进行httpsauthentication,如下所示:

var https = require('https'); var fs = require('fs'); var options = { pfx: fs.readFileSync('./8ab20f7b-51b9-4c09-a2e0-1918bb9fb37f.pfx') passphrase: 'password' }; var server = https.createServer(options, function (request, response) { fs.readFile('index.html', function (error, data) { response.writeHead(200, {'Content-Type': 'text/html'}); response.end(data); }); }).listen(12345, function(){ console.log('server running'); }); 

但是当我用node.js启动这个代码时,我在我的Windows控制台中收到一条错误消息:

密码:“密码”

意外的标识符

我的代码与Node.js( http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener )的官方指南页非常相似,但是我无法启动我的https服务器。

我的密码有什么问题? (我在Windows 8 64位上运行node.js。)

我猜你的pfxpassphrase属性之间缺less的逗号是导致错误的原因。 在这里我加了逗号:

 var options = { pfx: fs.readFileSync('./8ab20f7b-51b9-4c09-a2e0-1918bb9fb37f.pfx'), passphrase: 'password' }; 

我坚持承诺包装在我的实施,并保持asynchronous(ES2015)。

LIB / pfx.js

 import { readFile } from 'fs' import { resolve as resolvePath } from 'path' export const CERTIFICATE_ROOT = resolvePath(__dirname, '..', 'etc', 'certificates') export const getCertificatePath = filename => resolvePath(CERTIFICATE_ROOT, filename) export function readCertificate(filename) { let certificatePath = getCertificatePath(filename) return new Promise((resolve, reject) => { readFile(certificatePath, (err, certificate) => { if (err) return reject(err) resolve(certificate) }) }) } export function readPfx(filename, passphrase) { assert.typeOf(passphrase, 'string', 'passphrase must be a string') assert.isAbove(passphrase.length, 0, 'passphrase must not be empty') return readCertificate(filename).then(pfx => ({ pfx, passphrase })) } 

和用法

LIB / app.js

 import { readPfx } from './pfx' readPfx('8ab20f7b-51b9-4c09-a2e0-1918bb9fb37f.pfx', process.env.PASSPHRASE) .then(opts => /* start server here */) .catch(err => /* handle errors */)