0

I'm trying to make a new endpoint everytime a node is connected to my server, and will be used only by that endpoint. But, right now I'm stucked in how to generate a new endpoint while the node.js program is running.

Here is a little ilustration of how I want it to works : First, the server (node.js) is up. (Endpoint => /start) Second, the node sending it's own uid. Third, the server received the data and make a new endpoint based from the node uid it's received. (Node uid = aaaa, New endpoint => /aaaa) Fourth, everytime a new node is up, it will back to the second step.

I can't any solution or references about this in the internet ...

Any advice ?

Thank you.

Lyncheese
  • 13
  • 3
  • How are you currently setting up endpoints like `/start`? Are you using any libraries/frameworks? If you're using a basic [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server), it'll receives every request made to the server with the [`url` path and query-string](https://nodejs.org/api/http.html#http_message_url) simply being a string property. And, it's just as much up to you to define which paths aren't valid as those that are. – Jonathan Lonowski Apr 08 '15 at 17:52
  • Currently, I'm using the modules from node.js which is called Express. The Express is the modules inside npm packages. To make a new endpoint, simply I just need to write "express.get("/start")", or something like that.... Actually, I'm not quite understand about what you're talking about since I'm a beginner in server things. But, I'll look forward to it and see what I can get... Thank you for your response anyway.. highly appreciated ! – Lyncheese Apr 10 '15 at 03:12
  • With Express, you can use a parameterized route -- `app.get('/:uid', ...);`. You can retrieve its value with [`req.params`](http://expressjs.com/4x/api.html#req.params) and preprocess it with [`app.param()`](http://expressjs.com/4x/api.html#app.param) and/or [`router.param()`](http://expressjs.com/4x/api.html#router.param). – Jonathan Lonowski Apr 10 '15 at 04:14
  • Will this work to multiple nodes which connected to the server in node.js? For example, we have 10 nodes connecting to 1 server. Will the server generates all the endpoints for the 10 nodes ? – Lyncheese Apr 10 '15 at 08:06
  • Yes, one route can handle all 10+ nodes. My point before is that the path is just a string to the server -- `console.log(req.url); // "/aaaaa"`. Express handles routing by converting the route you give into a RegExp, then matching that against against the path string. `"/:uid"`, for example, will match any path string with a single segment (separated by slashes) -- `"/foo"`, `"/bar"`, `"/aaaaa"`, etc. while skipping `"/foo/bar"`, etc. You'll have to actually handle it the other way around, checking the `req.params.uid` and refusing any that don't match an active node. – Jonathan Lonowski Apr 10 '15 at 14:19
  • Honestly, I'm not quite confident it will works. Let's say that when I post to app.post("/uid1/postdata"), then the app.get("/:uid/data") will become app.get("/uid1/getdata"). But, what if when there are 2 nodes posting to app.post("/uid1/postdata"), and app.post("/uid2/postdata")... When I want to get the uid1 data, because the last post is in uid2 data, instead of uid1 data the server will give me the uid2 data.... bcause in app.get("/:uid/getdata") the :uid will be uid2 not uid1 anymre, it's already "changed". I hope you know what I mean.. Do you have any opinion about this ? – Lyncheese Apr 12 '15 at 03:25

1 Answers1

0

There are two NPMs maybe you can try, both packages are automatically detect all the endpoints and list them with METHODS (like GET, POST, DELETE etc)

https://www.npmjs.com/package/api-doc

https://www.npmjs.com/package/express-list-endpoints

Hope this will help for someone.

mapmalith
  • 1,303
  • 21
  • 38