7

I'm beginning to study node js and im trying to connect an ionic app with my backend nodejs app that i have created but im getting this error:

OPTIONS 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)
});
1
  • Are you running a HTTPS server on localhost / port 3000? Commented Sep 19, 2019 at 4:27

1 Answer 1

18

Port 3000 is commonly used for plain HTTP and not HTTPS and this is likely true in your setup too. It does not magically turn into HTTPS if you use https:// instead of http:// in the URL.

The error messages is a result of your client speaking the wrong protocol (HTTPS, i.e. HTTP over TLS) and that the answer from the server (plain HTTP, without TLS) is not the valid TLS message which is expected by the client - i.e. ERR_SSL_PROTOCOL_ERROR.

`

Sign up to request clarification or add additional context in comments.

5 Comments

So how to fix it?
@MuktadirKhan: If port 3000 is not HTTPS but HTTP then you should not use https://...:3000/... to access it in the code but http://...:3000/... instead.
Which port should be used for HTTPS?
@WashingtonGuedes: "Which port should be used for HTTPS" - ports don't magically turn up to be HTTPS, so the question should not be which port to use but how to configure HTTPS in the server. Then use the port which has HTTPS configured. Common choices are ports with something like 443 in it (like 8443) since 443 is the default port for HTTPS.
Actually, port 3000 worked here when I added the https module following this post: github.com/nuxt/nuxt.js/issues/146#issuecomment-273509831 .. my use case is just to test secure cookies for SSO in localhost domains, but thank you for the explanation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.