make the bundle available on any host (0.0.0.0) - express

I am trying to access localhost in my android phone.
I am using webpack-dev-middleware with an express server.
I do know how to set the host in webpack-dev-server though.
But I don't see any options to specify a host of 0.0.0.0 in webpack-dev-middleware.
Hope that my problem is clear enough

app.listen(3001, 'localhost', function() {
console.log("... port %d in %s mode", app.address().port, app.settings.env);
});
This the way you specify port and host . If you don't specify localhost then it's default exposed in 0.0.0.0
So below code listens in 0.0.0.0
app.listen(3001, function() {
console.log("... port %d in %s mode", app.address().port, app.settings.env);
});
Update:
You can set your ip address directly in webpack config file:
devServer: {
host: '0.0.0.0',//your ip address
port: 8080,
disableHostCheck: true,
...
}

Related

API Call works for IPv4 Address (127.0.0.1) but get a "Error: connect ECONNREFUSED ::1:3001" when using localhost

So I have a very simple API call using fetch on my frontend to http://localhost:3001/test that gives me an error: Error: connect ECONNREFUSED ::1:3001
However, when I call that API directly (enter the api uri directly into my browser), it works just fine. Also when I change localhost to http://127.0.0.1:3001/test on my frontend fetch call, that works too.
This seems like it's gotta be a network error since ::1 and 127.0.0.1 resolve to the same address but one is IPv4 and the other is IPv6 right? Anyone have any thoughts on why this could be?
frontend fetch (BACKEND_URL = http://localhost:3001):
export async function getStaticProps() {
const res = await fetch(`${BACKEND_URL}/explore`, {
method: 'GET',
headers: {
"Content-Type": 'application/json',
Origin: BASE_URL,
},
});
...
}
Backend Server listening on port 3001 (PORT = 3001):
const PORT = process.env.PORT;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
});
Stack: NextJS frontend, ExpressJS backend, MongoDB Atlas DB, NextAuth for auth
A couple of things can be the issue:
You need to enclose the IPv6 address between brackets, like http://[::1]:3001/test
The service is only listening on the IPv4 address and not on the IPv6 address. Depending on the server you may need to configure the listening address differently
Your post doesn’t contain enough information to go into more detail. Please edit your post to include the actual code, service and configuration so we can help you further.
When you use "localhost" in the backend URL, that is by default going to be resolved to the IPv6 address ::1. However, looking at the backend server code, that backend is listening on 0.0.0.0 which is IPv4.
You need to make that backend listen on IPv6:
const PORT = process.env.PORT;
app.listen(PORT, '::', () => {
console.log(`Server is running on port ${PORT}`);
});

How to expose RSK node to an external network?

I am having problems exposing my RSK node to an external IP.
My startup command looks as follows:
java \
-cp $HOME/Downloads/rskj-core-3.0.1-IRIS-all.jar \
-Drsk.conf.file=/root/bitcoind-lnd/rsk/rsk.conf \
-Drpc.providers.web.cors=* \
-Drpc.providers.web.ws.enabled=true \
co.rsk.Start \
--regtest
This is my rsk.conf:
rpc {
providers {
web {
cors: "*",
http {
enabled = true
bind_address = "0.0.0.0"
hosts = ["localhost", "0.0.0.0"]
port: 4444
}
}
}
}
API is accessible from localhost, but from external network I get error 400. How do I expose it to external network?
You should add your external IP to hosts. Adding just 0.0.0.0 is not enough to indicate all IPs to be valid. Port forwarding needs to be enabled for the port number that you have configured in rsk.conf, which in this case is the default value of 4444.
rpc {
providers {
web {
cors: “*”,
http {
enabled = true
bind_address = “0.0.0.0"
hosts = [“localhost”, “0.0.0.0", “216.58.208.100”]
port: 4444
}
}
}
}
where 216.58.208.100 is your external IP

peerjs working on loaclhost but not on heroku?

I'm running my node server on 3000 port and peer server on port 3001.In this scenario its working properly.But when deployed over heroku i'm running my server at 3000 and peer server over 443. In this scenario peerjs not wroking. It might be port alloction issue i guess but i'm unable to find the issue.
peer.js
const myPeer = new Peer( {
secure:true,
host: 'my-app-name.herokuapp.com',
port: 443
})
server.js
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
github link to project : link
New to Heroku. Any help will be appreciated!
Add this to your server file:
var ExpressPeerServer = require("peer").ExpressPeerServer;
var options = {
debug: true,
allow_discovery: true,
};
let peerServer = ExpressPeerServer(server, options);
app.use("/peerjs", peerServer);
And call on client side like this:
var peer = new Peer({
host: "yoursite.herokuapp.com",
port: "",
path: "/peerjs",
});
You Have to Host Two Apps on Heroku. First Your Main App and Second Your PeerJS Server. Because You Cannot Host your App On different Port (i.e. https://your-app-name.herokuapp.com:5000). And Then You can Connect Your Main App PeerJS Client With Your PeerJS Server by using this.
const myPeer = new Peer( {
secure:true,
host: 'my-peerjs-server-name.herokuapp.com',
port: 443
})
Happy Coding!
Just use this Heroku Element to deploy your own peer server with zero configuration. Connect to it from your client providing the host attribute as the url of your Heroku app without the https:// part and you may need to also set secure to true.
{
host: "you_app_name.herokuapp.com", // exclude protocol
secure: true
}
add this in server(index,app) file
const { ExpressPeerServer } = require("peer")
const peerServer = ExpressPeerServer(server, {
debug: true
})
app.use("/peerjs", peerServer);
and in client side add this
const myPeer = new Peer(undefined, {
path: "/peerjs",
host: "/",
port: "443",
})
port should same as your server.listen(port)
This will give invalid frame header for socket io but its fine

Trying to run npm run serve (vue-cli app) inside Laravel Homestead (VirtualBox + Vagrant)

I have a vue-cli app, trying to run it inside Laravel Homestead.
What I have:
My hosts on host machine:
127.0.0.1 localhost
127.0.1.1 PC
192.168.2.10 myvueapp.local
hosts inside VM:
127.0.0.1 localhost
127.0.0.1 myvueapp.local
127.0.1.1 homestead homestead
Vagrant version: 2.2.4, Homestead: v8.3.2, vue --version: 3.7.0
npm run serve executes without problems inside VM, but I get
We're sorry but myvueapp doesn't work properly without JavaScript
enabled. Please enable it to continue.
as a response body from request:
//response headers
Request URL: https://myvueapp.local/
Request Method: GET
Status Code: 200
Remote Address: 192.168.2.10:443
Referrer Policy: no-referrer-when-downgrade
Browser page is blank.
Also there is one favicon request:
Request URL: https://myvueapp.local/%3C%=%20BASE_URL%20%%3Efavicon.ico
Request Method: GET
Status Code: 400 Bad Request
Remote Address: 192.168.2.10:443
Somehow BASE_URL doesn't compile in index.html:
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
My vue.config.js:
module.exports = {
devServer: {
host: 'myvueapp.local',
https: true
}
}
Homestead.yaml:
ip: "192.168.2.10"
#...
sites:
- map: myvueapp.local
to: /home/vagrant/path/to/myvueapp.local/public
#...
ports:
- send: 8080
to: 80
Port, where Vue is served (inside VM, 8080) is listening.
lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 3022 vagrant 22u IPv4 31440 0t0 TCP localhost:http-alt (LISTEN)
Nginx config:
server {
listen 80;
listen 443 ssl http2;
server_name .myvueapp.local;
root "/path/to/myvueapp.local/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
P. S. It runs ok when I'm serving it from my host machine.
What I've tried:
change host
module.exports = {
devServer: {
host: '0.0.0.0', //<-- here
https: true
}
}
, didn't helped.
Edit #1
I moved a bit further, this Nginx config now allowes me to access Vue app, served inside VM, using host machine:
location / {
try_files $uri $uri/ /index.html =404;
proxy_pass http://localhost:8080; #<-- this might be the output from npm run serve, without last slash
# App running at:
# - Local: http://localhost:8080/
# ^^^^^^^^^^^^^^^^^^^^^
}
But there is still a problem: hot-reload doesn't work.

How to fix "errno":"ENOTFOUND" error in smtp configuration using express js

I am trying to configure node mailer in express js application. During the time of sending mail, I got an error ENOTFOUND.
My smpt configurations are,
smtpServer: {
host: "smtp.gmail.com",
port: 587,
secure: false,
user: "email#gmail.com",
pass: "password",
fromName: "",
fromAddress: ""
}
Error :
{"errno":"ENOTFOUND","code":"ECONNECTION","syscall":"getaddrinfo","hostname":"smtp.gmail.com","host":"smtp.gmail.com","port":587,"command":"CONN","meta":{"errno":"ENOTFOUND","code":"ECONNECTION","syscall":"getaddrinfo","hostname":"smtp.gmail.com","host":"smtp.gmail.com","port":587,"command":"CONN"}}
I suspect this error came because of proxy issues. So, I checked the smpt hostname in my machine nslookup table.
That is returned following result:
$ nslookup smtp.gmail.com
Server: 127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
*** Can't find smtp.gmail.com: No answer
Other than else what I missed here?