StackBlitz - ECONNREFUSED when connecting to local machine

171 views Asked by At

I'm running a simple express server on my machine and attempting to ping it from a StackBlitz web container, but am getting the following error when trying to connect:

FetchError: request to https://192.168.1.23:8443/ failed, reason: connect ECONNREFUSED 192.168.1.23:8443

I've tried:

  • using https
  • allowing cors
  • setting up host: 0.0.0.0

Here's the code for the server, its a vanilla node project, just with express and cors.

var fs = require('fs');
var http = require('http');
var https = require('https');
var cors = require('cors')
var privateKey = fs.readFileSync('key.pem', 'utf8');
var certificate = fs.readFileSync('cert.pem', 'utf8');

var credentials = { key: privateKey, cert: certificate };
var express = require('express');
var app = express();
app.use(cors())

app.get('/', (_, res) => res.send(`hello`));

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080, '0.0.0.0');
httpsServer.listen(8443, '0.0.0.0');

Here is the code for the stackblitz container, it is a vanilla node project with node-fetch:

import fetch from 'node-fetch';

const url = 'https://192.168.1.23:8443/';

const response = await fetch(url)
  .catch((err) => console.error(err));
console.log(response?.status || 'failed');
1

There are 1 answers

0
Erguotou On

Try use a html page to connect to your server. If still error then try embed an iframe in your html.