簡體   English   中英

為什么訪問使用Node.js / Express-TypeScript的SSL證書時遇到問題?

[英]Why am I having trouble accessing SSL certificates working with Node.js/Express-TypeScript?

我正在嘗試將項目的后端從JavaScript(Node.js / Express)切換到TypeScript,但是我遇到了一個很奇怪的問題。 FS的readFileSync無法/不會訪問安全文件夾中的key.pem或cert.pem文件(與server.ts文件處於同一項目級別),這些文件是為localhost https生成的。 這在我的原始應用程序中可以正常工作,那么我缺少切換到TS的功能嗎?

這是相關的代碼片段。

import * as https from 'https';
import * as fs from 'fs';

import app from './app';
import { PORT } from './config';

const httpsOptions = {
  key: fs.readFileSync('./security/key.pem'),
  cert: fs.readFileSync('./security/cert.pem')
};

https.createServer(httpsOptions, app).listen(PORT, () => {
  console.log('Express server listening on port ' + PORT);
})

下面是拋出的錯誤消息的快照。

在此處輸入圖片說明

我猜您編譯的TypeScript代碼不在某個可以使用帶點符號的相對路徑的地方。 您的TS來源可能位於根文件夾中,這里也有security文件夾。 但是build文件夾中沒有包含JS的security文件夾。

使用Node的path本機模塊或將security文件夾放入構建中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM