【发布时间】:2020-04-02 12:25:16
【问题描述】:
对我的 NestJS 应用进行 API 调用时,我收到以下错误。
core.js:6185 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "https://localhost:3333/api/product-index", ok: false, ...}
和
GET https://localhost:3333/api/product-indexnet::ERR_SSL_PROTOCOL_ERROR
通过单独研究 Nest、Angular 和 NGXS 以及建议的处理方式,我已经正确设置了所有内容。我唯一能想到的就是修改对localhost:3333/api 的引用,看看我是否定位到一个不存在的位置。我注意到当我将https 更改为http 时,我得到一个CORS 错误,即使在main.ts 文件中包含enableCors() 也不会消失。我已经观看并阅读了一些将 NestJS 连接到 Angular 的教程,一旦他们设置了文件,它就可以工作了。我一直在查看 nrwl 网站上的教程,据我所知,我已经正确设置了所有内容。
这就是我的 Nest 应用中 main.ts 文件的样子
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.port || 3333;
await app.listen(port, () => {
console.log('Listening at http://localhost:' + port + '/' + globalPrefix);
});
}
bootstrap();
angular.json 文件中我的前端项目的 serve 数据如下所示
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "cre8or-maker:build",
"proxyConfig": "apps/cre8or-maker/proxy.conf.json"
}
我的前端项目中的proxy.conf.json 文件如下所示
{
"/api": {
"target": "https://localhost:3333",
"secure": false,
"logLevel": "debug"
}
}
我的 NGXS 模块中有一个服务文件,它向 Nest 应用程序中的 product-index 控制器发出请求,如下所示
export class ProductIndexService{
constructor(private httpClient: HttpClient){}
private readonly URL: string = 'https://localhost:3333/api';
public fetchProductIndexList():Observable<CattegoryIndexItem[]>{
const path: string = this.URL + '/product-index';
return this.httpClient.get(path) as Observable<CattegoryIndexItem[]>;
}
}
我的environments/environments.ts 文件
export const environment = {
production: false,
apiUrl: '/api'
};
我的environments/environments.prod.ts 文件
export const environment = {
production: true
};
正如我之前提到的,我尝试了诸如在路径中添加和删除 /api 以及在 http 和 https 之间来回切换之类的操作,但它不起作用。我可以从localhost:3333 成功调用product-index 控制器,所以我知道Nest 中的所有设置都已正确设置,从错误中显示的路径来看,我似乎从我的NGXS 状态正确定位它。这里有什么问题?我错过了什么,或者我应该看看?
【问题讨论】:
-
只是猜测。 Nest 的自签名证书存在问题。尝试在您的服务器端代码中添加此代码:process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
-
我应该将它添加到哪个文件中?
标签: angular typescript nestjs ngxs nrwl-nx