0

I am receiving the following error: (index):24 POST https://goldengates.club:3000/api/transact net::ERR_SSL_PROTOCOL_ERROR (anonymous) @ (index):24 goldengates.club/:1 Uncaught (in promise) TypeError: Failed to fetch Promise.then (async) (anonymous) @ (index):26 when trying to make a post request from client side to my api backend. I have opened port 3000 with express to take requests on an endpoint named api/transact where the client posts data to. The problem seems to be with my current ssl but I don't know what the problem is specifically. I checked in with the server provider and they told me quote "You may need to reconfigure your application running on port 3000 to use SSL to encrypt data. If you require certificate files, they should be available in ssl directory in the user's account." Incase this gives a hint.

Relevant Server API code:

const Blockchain = require('./blockchain');
const PubSub = require('./app/pubsub');
const TransactionPool = require('./wallet/transaction-pool');
const Wallet = require('./wallet');
const TransactionMiner = require('./app/transaction-miner');
const PubSubNub = require('./app/pubsub.pubnub');
//127.0.0.1:6379
const isDevelopment = process.env.ENV === 'development';
//TRY PUBNUB (comment out)
/*const REDIS_URL = isDevelopment ?
  'redis://127.0.0.1:6379' : //try 6379 19289
  'redis://h:p602b6838e89da65c8c4d29a6a4f954452d1ece59c10b27a29ebf9808721cb8e2@ec2-35-153-115-238.compute-1.amazonaws.com:9819'//19289
*/  
const DEFAULT_PORT = 3000;
const ROOT_NODE_ADDRESS = 
`http://localhost:${DEFAULT_PORT}`;

const app = express();
const blockchain = new Blockchain();
const transactionPool = new TransactionPool();
const wallet = new Wallet();
//const pubsub = new PubSub({ blockchain, transactionPool, redisUrl: REDIS_URL });//redis
const pubsub = new PubSubNub({ blockchain, transactionPool, wallet }); // for PubNub //change back to PubSub if issues arise
const transactionMiner = new TransactionMiner({ blockchain, transactionPool, wallet, pubsub });
//DELETE THIS
/*const config =
{
    headers: {'Access-Control-Allow-Origin':'*'}
};*/
//TEST DELETE

app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client/dist')));
//delete below
//app.options('*', cors());

app.use(cors({
    origin: 'https://goldengates.club',
    allowedHeaders: ["Content-Type"],
    methods: 'POST'
}));

SKIPPING TO FoCUSED ENDPOINT...

app.post('/api/transact',(req, res) => {

  const { amount, recipient } = req.body;
  //REMOVE
  console.log(amount);
  //REMOVE
  let transaction = transactionPool
    .existingTransaction({ inputAddress: wallet.publicKey });

  try {
    if (transaction) {
      transaction.update({ senderWallet: wallet, recipient, amount });
    } else {
      transaction = wallet.createTransaction({
        recipient,
        amount,
        chain: blockchain.chain
      });
    }
  } catch(error) {
    return res.status(400).json({ type: 'error', message: error.message });
  }

  transactionPool.setTransaction(transaction);

  pubsub.broadcastTransaction(transaction);

  res.json({ type: 'success', transaction });
});

Client side code:

<!DOCTYPE html>

<html>
    <head>

    </head>
    <body>

    <script>
        const amount=10;
        const recipient=1221232;
        const data={amount,recipient};

        const options=
        {
            method: 'POST',
            headers:
            {
                'Content-Type': 'application/json',
            },

            body: JSON.stringify(data)
        };
        fetch('https://www.goldengates.club:3000/api/transact',options)
        .then(response => response.json())
        .then(data => console.log(data));
    </script>
    </body>
</html>
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

1 Answers1

0
https://goldengates.club:3000/api/transact net::ERR_SSL_PROTOCOL_ERROR    

Your application on port 3000 is not setup for HTTPS but only for HTTP. Trying to access a plain HTTP server with HTTPS results in the error you see. Note that you cannot simply switch http://host:port with https://host:port in the client and expect the server to be magically available with HTTPS.

... they told me quote "You may need to reconfigure your application running on port 3000 to use SSL to encrypt data. If you require certificate files, they should be available in ssl directory in the user's account."

They are pointing out exactly the same.

Apart from that a common way to expose a nodejs API with HTTPS is not to make the nodejs server itself be capable of HTTPS but to hide it against some reverse proxy like nginx - see for example Configuring HTTPS for Express and Nginx.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • hey, thanks for the reply back. Can configuring my port 3000 to run on https be achieved with apache? –  Jun 20 '20 at 14:38
  • I have fixed the error, I added a connection option to connect through an ssl. I added the following object to use when listening on a particular port... ```const httpsOptions = { cert: fs.readFileSync(path.join(__dirname,'ssl/certs','goldengates_club_ad6a3_c1c83_1599436799_f7d90f2bde299ad7cb014f9ef76de722.crt')), key: fs.readFileSync(path.join(__dirname,'ssl/keys','ad6a3_c1c83_17344a543062170b02640b27891d7770.key'))};``` –  Jun 20 '20 at 16:14
  • ```const PORT = process.env.PORT || PEER_PORT || DEFAULT_PORT; /*app.*/https.createServer(httpsOptions, app).listen(PORT, () => { console.log(`listening at localhost:${PORT}`); if (PORT !== DEFAULT_PORT) { syncWithRootState(); } }); ``` –  Jun 20 '20 at 16:15
  • following libraries are needed... npm i https and npm i fs –  Jun 20 '20 at 16:16