Need Node app to watch external website instead of localhost - node.js

I'm still new to Node, so what I'm asking may not work the way I'm wanting to, but, here is my dilemma.
I have a website which has an old data collector (which I did not write) collecting data. I wrote a Node app that mimics the old data collector so that it can be replaced. But now that it's ready for testing, how do I point the Node app towards the website and not localhost? The Node app is going to be hosted in a secure server.
When I would test in Postman I would test the functionality for, say, the 'id' endpoint by checking
localhost:3000/id
but now I want is when a user on the website goes to an address such as
www.myexample.com/id
The code in my Node app will run. And I may be wording this wrong, but basically if one of the endpoints is hit, I want Node to run the code for that endpoint.
The code for my endpoints is along these lines:
router.post('/id.json', function (req, res, next) {
//do the things
}
Is there a way to have Node work this way with an external website? I've checked, but haven't come across anything that would work for this particular issue. I'm using Express and I've tried changing my app.listen, to
app.listen('www.example.com')
but I'm getting errors from there, so I'm not sure if I'm not using proper syntax or if this simply isn't what app.listen was intended to do. Ultimately, what I'm wanting to do is have the Node app work the same way with the website as it would with localhost.

You can't take any site name by just "listening" to it with node app. When user in the Internet goes to www.myexample.com/id, the url is resolved into ip address and user's browser connects to a server (physical machine) that is running on that ip address. This server then accepts the connection and serves the website back to user. If you own www.myexample.com domain name and the server this domain name points to, you should go to the server, take down whatever is hosting your current website and run your node app there. Your node app doesn't even know which website address it's being hosted on, all it cares about is accepting incoming connections and returning data. Also mind the port - http and https work on ports 80 and 8080 respectively (which are omitted in the url) and your node app, based on your description, is running on 3000

Related

How to retrieve Nodejs/Express web server port number from a loaded-in-browser Javascript file

My situation is the following:
I have a Node/Express server.js file which gets run by Node.js. This backend is for my website, which I deploy to Heroku. The webserver serves two endpoints, from either const PORT = process.env.PORT || 8000:
'/', or root (this responds by sending file index.html)
'/transcription'
When a request is made to /transcription, an Axios post request is made to a third-party API to get a temporary token. This token is passed into the response from /transcription as res.json(data). No problems so far.
The trickiness enters here. I have JavaScript file, asr.js, which is loaded when I visit page asr.html (different from index.html). In asr.js, I fetch http://localhost:8000/transcription. Now, this works locally for me; I get my temporary token and continue with my transcription. However, when I push this to an environment like Heroku, where I don't know what the port number is going to be, I cannot successfully make that fetch request to neither http://localhost:8000/transcription nor http://localhost:${PORT}/transcription because process is not defined in my asr.js file; it's not being executed by node.
So, the question I have is how can I determine what my Node/Express server port is when I deploy my app to Heroku such that I can use it in asr.js when I visit asr.html?
Or maybe that's not the right question to be asking? Community, please help!
Here is a snapshot of my folder hierarchy, for more context. Let me know if more information is required or if my initial explanation is not enough.
To be clear, I use middleware to statically serve the public folder.
Unless I'm missing something here, why would you need the port in production?
Very rarely is node run as the webserver, typically you will have NGINX proxy the request to the backend server so that port is only valid to the NGINX server to proxy those request back to node, otherwise your app would not work with a TLD without the port.
Long story short, your frontend JS should not need the port number and be able to just use the TLD running on port 80 / 443

Setting up internal web server with Node.js

I want to host a web app with node.js on a Linux virtual machine using the the HTTP module.
As the app will be visualising sensitive data I want to ensure it can only be accessed from PCs on the same LAN.
My understanding is that using the HTTP module a web server is created that's initially only accessible by other PCs on the same LAN. I've seen that either by tunnelling or portforwarding a node.js server can be exposed if desired.
Question
Are there any other important considerations/ways the server could be accessed externally?
Is there a particular way I can setup a node.js server to be confident that it's only accessible to local traffic?
It really depends what you are protecting against.
For example, somebody on your LAN could port forward your service using something like ngrok. There are a few things you can check for:
In this case the header x-forwarded-for is set. So, to protect against this you can check for this header on the incoming request, and if set you can reject the request.
The host header is also set and will indicate how the client referred to your service - if it is as you expect (maybe a direct local LAN address such as 192.168.0.xxx:3000) then all is OK, if not (I ran ngrok on a local service and got something of the form xxxxxxxx.ngrok.io) then reject it.
Of course a malicious somebody could create their own server to redirect requests. The only way there is to put in usernames and passwords or similar. At least you then known who is (allegedly) accessing your service and do something about it.
However, if you are not trying to pretect against a malicious internal actor, then you should be good as you are - I can't think of any way (unless there is a security hole in your LAN) for your service to be made public without somebody actively setting that up.
My last suggestion would be to use something like express rather than the http module by itself. It really does make life a lot simpler. I use it a lot for just this kind of simple internal server.
Thought I'd add a quick example. I've tested this with ngrok and it blocks access via the public address but works find via localhost. Change the host test to whatever local address (or addresses) you want to serve this service from.
const express=require('express');
const app=express();
app.use((req,res,next)=>{
if (req.headers.host!=='localhost:3000' || req.headers['x-forwarded-for']){
res.status(403).send('Invalid access!');
} else next();
});
app.get('/',(req,res)=>res.send('Hello World!'));
app.listen(3000,()=>{
console.log('Service started. Try it at http://localhost:3000/');
});
I would prefer using nginx as a proxy here and rely on nginx' configuration to accept traffic from local LAN to the node.js web server. If this is not possible, a local firewall would be the best tool for the job.

Does a Node js web server need a domain name to communicate with clients on other devices?

I am working on a swift project for osx with Firebase. I have a node web server to communicate between the clients and the Firebase-server, but it's a localhost-server. Do I need a real domain name to make the server accessible to end-users on another device? (I don't want a web app, just the backend for myself)
you doesn't need a domain .. but you need a serve to deploy having ip address .. suggestion you can use cloud server
You have two ways:
make request on port that the nodejs uses, example http://101.01.01.01:8000
use nginx like proxy, in this setup make your requests on 80 port (it's default), example http://101.01.01.01.
If you wont make something like dev environment on local machine use first case (don't forgot open port for other devices), for production - second.

node js send html to network rather than only localhost server

I'm using node js trying to send my web-page to my network, I successfully call localhost:port in my computer using express as server, the webpage loads fine trigger my webcam which I used to streaming in the webpage, and then im working to make a simple app in my phone to directly access my server, so my questions:
1.How do I able to access my server from different devices in the same wireless-network? by calling ip + port ?192.168.1.104:9001 ? cause i've tried and it didnt work.
2.I've found https with .pem something like that, is that the answer ? is there also any other way ?
3.maybe any advice before i work to make my web-app to devices? using koa? i don't even really know what is that, but i'm happily take any advices.
EDIT: i've read How could others, on a local network, access my NodeJS app while it's running on my machine?
let's say I simply using random router, so i can't configure my router-port, my server in my pc and my phone join in the same network, trying to access the server in my phone
1.How do I able to access my server from different devices in the same wireless-network?
All you need to do is find your server's IP address in this same wireless-network, and find the Node.js application's port. Then access the following URL in other devices:
http://{server_IP}:{port}
However, there are some points need to check:
Need to check firewall and confirm the port is not blocked, server IP is not blocked by test device, and test device IP is not blocked by server.
Need to check whether there is any Proxy setting in server and test device. If there is any, disable the proxy.
A computer may have many IP addresses at the same time, you need to find the correct one in the same wireless-network. For example, If you install a virtual machine software such as VMware and run a virtual system inside, your real computer will get IP address as 192.168.*.* -- this IP address looks like an intranet IP in wireless-network, but it is not, and can never be accessed by test device.
2.I've found https with .pem something like that, is that the answer?
No, HTTPS has nothing to do with this problem. HTTPS just add security (based on HTTP layer), it does not impact any HTTP connectivity. Actually, to minify the problem, it is better to only use HTTP in your scenario.
There is only one very special case that may bring your problem by HTTPS -- the test machine is configured and will block any non-HTTPS connection for security.
3.maybe any advice before i work to make my web-app to devices? using koa?
My suggestion is: As there is an HTTP connectivity issue, the first step is trying to find the root cause of that issue. Thus, it is better to make a simplest HTTP server using native Node.js, no Koa, no Express. In this way, the complexity of server will be reduced, which makes root cause investigation easier.
After the HTTP connectivity issue is fixed, you can pick up Koa or Express or any other mature Node.js web framework to help the web-app work.
4.let's say I simply using random router, so i can't...
Do you mean your server get dynamic IP address by DHCP? As long as the IP is not blocked by test device, it does not matter.

How to setup an API server with no domain name

I've been struggling to understand this because I don't quite know what to search for. Basically, I'm working on a simple node server that just works as an API that is to be consumed by a mobile application. I'm planning to deploy it to DigitalOcean but since I don't need a domain name because I don't have a website, how will I send the HTTP requests to the server? My guess is something related to the droplet's IP but that doesn't seem quite right.
Just send requests to IP address and port from your mobile app. like GET http://54.54.32.23:3000/user-newsfeeds/15

Resources