0

I am new to Node.js 6 and Express 4. I am wondering if something like this is possible todo? It appears wildcards can be used in the routeing of node. Is it possible to have a database driven app that is dynamic with routes and views? What I mean is something like the following URL's

  1. / <- can be anything
  2. /xyz
  3. /15/abc/xyz

So node/express would hit the database for the URL of / and then dynamically take the values in the row for / lookup it found, and output the path to the view template page with the SQL query ready to be used in the view template file that is local on disk. I know their is no way to dynamically generate the html because the SQL will be different for each view template URL. So that would haft to be a hard file with template engine like handlebars, etc. It appears node/express can dynamically deal with routes on the fly so this should be possible todo I think.

So when node/express get the URL of /xyz it will go into the database look up the URL and then output the SQL in the lookup row and call the path to the view template for that row it found in the database. Database could be a json file not sql too. Do not know what would be faster since both would be in RAM.

I am wondering if anyone has ever tried this? If they have dose anything like this or dose anyone know of a boilerplate with this kind of a setup on github? I can see several problems.

  1. Handling 404 errors
  2. Database pools, Ways to reduce the open and closing sockets. So when 100 URL requests would not have a 1,000 open and close socket requests. It would have just 1 open socket request and do all the SQL via that socket. Or have 64 sockets for 64 cpu system. Not open and closing socket every time you hit the URL.
  3. Run app under PM2 Clustering so it will use all the CPU's not just one CPU.

I would like any input. How you would over come the problems listed or boilerplate to something like this if it is out their already?

1 Answers1

0

What you're describing is a RESTful API. Generally, the very first item in the URL path is static if you're serving webpages, since otherwise you wouldn't be able to serve anything else from the root path, such as -- like you noticed -- error pages. So you'll commonly see URLs like www.mystore.com/products/1234.

Your #2 and #3 have nothing to do with routing. Connection pools don't work how you think: it's about reusing and managing the lifespans of many connections, which are picked up and released by your app as needed. You don't want to be sending all your SQL over one socket (since a long-running query would halt everything else until it completed), and the number of open sockets isn't limited by how many CPUs you have.

Clustering is just as possible with a RESTful app as it is with a non-RESTful one.

Community
  • 1
  • 1
dmfay
  • 2,199
  • 1
  • 11
  • 18
  • I did look at rest but I could not see how rest could be modify with template views like ejs, handlebars on the views from the database selects. So the each view would have it's own database query to output. How I understand rest is it output json or data back to the requesting service. So I kinda see what you are talking about but I do not see how to make it work dynamically via URL / Database. I am trying to get away from hard coding any URL in node. It will hit database for all URL's. App will be almost 100% database driven with node code will be very very small. What I am after. – user2777145 Feb 05 '17 at 01:10
  • Rendering JSON or XML is only one possible output for a REST API. If your listener responds with a webpage via Express's `res.render` you'll output a webpage, and you can plug whatever template engine you like in. As for not adding *any* URLs, sure, you could just listen on / and tokenize the path, fire the appropriate queries, look up the appropriate template, and render it all out, but taking the manual work out of that is why you bring in Express in the first place. – dmfay Feb 05 '17 at 01:27
  • I am looking to see if I can do some way with out having a ton of files all over the place for routes. Wildcard with express and database look up is not working how I expect it too. Then doing a select and outputting that to res.render template engine with the query for access I cannot make work. So I am starting to think having a pure database driven URL's -> SQL -> Template View hard file is not possible. So all you have is app.get('*', dynamicURL) and that is all you have for all your routes in node. Is not possible to do... – user2777145 Feb 05 '17 at 01:37