无法将.pem文件导入到Express

我来自另一个没有得到答复的问题:

那么,我发现了一些新的东西。 我仍然不能导入我的证书,当我尝试执行我的节点/快速应用程序,它失败了相同的错误,但现在我认为,在某种程度上,FS包不正确读取我的.pem文件。

看一看:

// Setup HTTPS const httpsPort = 3443; const options = { key: fs.readFileSync("./key.pem"), cert: fs.readFileSync("./cert.pem") }; console.log("KEY: ", options.key) console.log("CERT: ", options.cert) var secureServer = https.createServer(options, app).listen(httpsPort, () => { console.log(">> CentraliZr listening at port "+httpsPort); }); 

我得到以下输出:

 C:\Zerok\dev\centralizr>node index.js KEY: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0d 0a 70 52 39 37 51 33 6f 50 5a 5a 59 75 39 46 6c 31 54 6d 30 0d 0a ... > CERT: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0a 4d 49 49 45 4a 54 43 43 41 36 79 67 41 77 49 42 41 67 49 49 48 48 ... > _tls_common.js:85 c.context.setKey(options.key, options.passphrase); ^ Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode at Error (native) at Object.createSecureContext (_tls_common.js:85:17) at Server (_tls_wrap.js:776:25) at new Server (https.js:26:14) at Object.exports.createServer (https.js:47:10) at Object.<anonymous> (C:\Zerok\dev\centralizr\index.js:29:26) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) 

正如你可能想象的那样,这与真正的证书和密钥文件看起来不一样。 他们有以下内容(没有问题显示他们,因为他们是免费的,自动签名的密钥只是为了testing目的):

在这里输入图像说明

在这里输入图像说明

那么…怎么了? 为什么Node读取这些文件为一个hex缓冲区? 我真的不明白。

更新:

好的,感谢Derek Brown,我现在可以看到这两个文件的内容,尽pipe我一直得到相同的错误:“糟糕的base64解码”:

 _tls_common.js:85 c.context.setKey(options.key, options.passphrase); ^ Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode at Error (native) at Object.createSecureContext (_tls_common.js:85:17) at Server (_tls_wrap.js:776:25) at new Server (https.js:26:14) at Object.exports.createServer (https.js:47:10) at Object.<anonymous> (C:\Zerok\dev\centralizr\index.js:29:26) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) 

如果没有指定编码, readFileSync(...)将返回一个hex缓冲区。 你应该指定编码为utf8 (或者你正在使用的任何文件编码),这样不会发生:

 const options = { key: fs.readFileSync("./key.pem", "utf8"), cert: fs.readFileSync("./cert.pem","utf8") };