I am using .pfx certification on my node.js https server and I cannot use the 'passphrase' option

I am currently building a web application with node.js and https. So I'm trying to use my .pfx (I got the file http://www.cert-depot.com/ here) for the certification required for https, for example, the following code:

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'); }); 

But when I run this code with node.js, I get an error message on my Windows console:

passphrase: 'password'

Unexpected id

My code is very similar to the official node.js man page ( http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener ), but I cannot start my https server.

What happened to my passphrase? (I am running node.js on Windows 8 64bit.)

+6
source share
2 answers

I think the missing comma between your pfx and passphrase properties is causing the error. Here I added a comma:

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

I use the promise wrapper for my implementation and keep it async (ES2015).

Library /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 })) } 

and use

Library /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 */) 
+2
source

Source: https://habr.com/ru/post/982078/


All Articles