This is a similar but different query to another question I asked back in 2019. I thought it sufficiently different to break out into a new question.
I have written a Node Express app, which uses CORS (Cross Origin Resource Sharing). I'm still trying to develop remotely (SSH to the server). VS Code is working well for that and I can SSH into the server and router (OpenWRT) for admin.
When I run the app, it tells me it's running. Using Links (command line browser) via SSH to the server, I can see the JSON response, so I know it's running.
I have run export PORT=8080
which is now correctly picked up by the code
The app code is:
// Based on a // Tutorial from https://www.bezkoder.com/react-node-express-mysql/ converted to access database of solar power generation values
const express = require("express");
const cors = require("cors");
const HOSTNAME = '0.0.0.0';
const PORT = process.env.PORT || 8082;
const app = express();
// const db = require("./app/models");
// db.sequelize.sync();
var corsOptions = {
origin: `http://${HOSTNAME}:${PORT}`
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
//const PORT = process.env.PORT || 8082;
// simple route
app.get("/", (req, res) => {
res.json({ message: `Welcome to Greg's application on localhost: ${PORT}.` });
});
// set port, listen for requests
app.listen(PORT, () => {
console.log(`Server is running on port:- ${PORT}.`);
});
For some reason, it's picking up the fallback port 8082, from const PORT = process.env.PORT || 8082;
, not the one specified in
var corsOptions = { origin:
http://${HOSTNAME}:8080 };
If I run curl -v -X -H http://<my static WAN IP>:8082/
from my remote terminal.
I get No route to host
- see DNAT rule, below
If I run curl -v -X -H http://<localhost> or <0.0.0.0>:8082/
from my server terminal via ssh.
I get
* Trying ::1:8082...
* TCP_NODELAY set
* Connected to localhost (::1) port 8082 (#0)
> -H / HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.66.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: close
<
* Closing connection 0
I believe I've set up a DNAT routing rule for port forwarding correctly (I've got three rules. One for each of 8080,8081 & 8082, for the purposes of experimenting) - edited (see also OpenWRT link for spec and link for explanation of 'option' vs 'list')
config redirect
option dest_port '8080'
option src 'wan'
option name 'Tranq8080'
option src_dport '8080'
option target 'DNAT'
option proto 'tcp'
option dest_ip '<server's LAN address>'
option dest 'lan'
I cannot find a way to access the app's http remotely. I don't know if it's the firewall, the code or some sort of cross scripting voodoo in the browser (the remote CURL refusal I think indicates it's not the latter)?
Help?