当我尝试与本地主机连接时,ERR_SSL_PROTOCOL_ERROR

lea*_*r29 1 ssl localhost node.js ionic-framework

我开始研究 node js,我尝试将 ionic 应用程序与我创建的后端 nodejs 应用程序连接,但我收到此错误:

选项https://localhost:3000/insert net::ERR_SSL_PROTOCOL_ERROR

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-cadastro-unidade',
  templateUrl: './cadastro-unidade.page.html',
  styleUrls: ['./cadastro-unidade.page.scss'],
})

export class CadastroUnidadePage implements OnInit {

  constructor(private http: Http) { }

  ngOnInit() {

  } 

  InsereDados(){
    let data = {
    };

    this.http.post('https://localhost:3000/insert', data).pipe(
            map(res => res.json())
        ).subscribe(response => {
            console.log('POST Response:', response);
        });
  }

}

app.get('/insert', function(req, res, next) {
var uri = "xxxx"
    MongoClient.connect(uri,{ useNewUrlParser: true }, function(err,client){const collection = client.db("test").collection("teste");
    console.log("connected");

    var ins={DataEntrada:"x",DataSaida:"x",HorarioEntrada:"x",HorarioSaida:"x",Prontuario:"x",TipoSaida:"x"};

    collection.insertOne(ins, function(err,res){
      console.log("data inserted");

    })

    client.close();
    })
    res.render(res)
});
Run Code Online (Sandbox Code Playgroud)

Ste*_*ich 7

端口 3000 通常用于纯 HTTP 而不是 HTTPS,这在您的设置中也可能是正确的。如果您在 URL 中使用https://而不是使用它,它不会神奇地变成 HTTPS http://

错误消息是由于您的客户端使用了错误的协议(HTTPS,即基于 TLS 的 HTTP)并且来自服务器的答案(纯 HTTP,没有 TLS)不是客户端预期的有效 TLS 消息 - 即ERR_SSL_PROTOCOL_ERROR.

`

  • 那么如何解决呢? (2认同)
  • @MuktadirKhan:如果端口 3000 不是 HTTPS 而是 HTTP,那么您不应该在代码中使用“https://...:3000/...”来访问它,而应该使用“http://...:3000/”。 ..` 相反。 (2认同)