I'm currently making an web application with node.js and https. So I try to use my .pfx(I got the file from here http://www.cert-depot.com/) for certification required for https, like 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 start this code with node.js, I'm getting an error message in my windows console:
passphrase: 'password'
Unexpected identifier
My code is very similar to the official guide page of Node.js (http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener), but I can't start my https server.
What's wrong with my passphrase? (I'm running node.js in Windows 8 64bit.)
I guess the missing comma between your pfx
and passphrase
properties is what cause the error. Here I added the comma:
var options = {
pfx: fs.readFileSync('./8ab20f7b-51b9-4c09-a2e0-1918bb9fb37f.pfx'),
passphrase: 'password'
};
I stick a promise wrapper on my implementation of it and keep it async (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 }))
}
and usage
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 */)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With