ChrisP
ChrisP

Reputation: 137

Need Node app to watch external website instead of localhost

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.

Upvotes: 0

Views: 234

Answers (1)

Max
Max

Reputation: 4739

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

Upvotes: 1

Related Questions