-1

No matter which library I use for node, they all require absolute URLs.

This means I need to either build a fetch curry, then pass that fetch through function chains to be able to make a request, OR I need to make a constant that is defined after figuring out whether im in production or development, then update that URL whenever my environments change.

Regardless, why does node require the hostname for URL's?

Is there a more seamless way to do server side requests anywhere in my app (including deep in function chains)?

A code example:

node index.js
index.js
let app = Express();
app.use('ace', (req, res) => foo());
app.use('somedata.json', (req, res) => res.status(200).send('{"hello": "world"}'));

foo.js
() => bar();

bar.js
() => bat();

bat.js
() => fetch('/somedata.json').then(console.log);
Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64
  • Who the heck downvotes without a comment? – Sophie McCarrell Oct 07 '16 at 16:03
  • I don't understand the actual problem that you're having. When making HTTP requests serverside, there is no such thing as a "base HREF" relative to which you can make requests, hence the need for absolute URL's. – robertklep Oct 07 '16 at 16:13
  • so express.listen doesn't set any globals for other libraries to look for relative to host urls. That answers my first question. It doesn't answer my second question "Is there a more seamless way t do server side requests anywhere in my app?" – Sophie McCarrell Oct 07 '16 at 16:36
  • Your example code would create an infinite loop. – robertklep Oct 09 '16 at 18:31
  • I don't see what you mean. How so? – Sophie McCarrell Oct 14 '16 at 04:52
  • A request for `/somedata.json` triggers the `*` handler, which calls `foo`, which calls `bar`, which calls `bat`, which performs a request for `/somedata.json`, which triggers the `*` handler, ... etc – robertklep Oct 14 '16 at 07:01
  • [Here are some thoughtful responses](http://stackoverflow.com/questions/7507015/get-hostname-of-current-request-in-node-js-express) to a similar question. – Jeff Lowery Jan 04 '17 at 00:49

1 Answers1

1

There is no such thing as a relative HTTP request. Any actual HTTP request must be a fully qualified URL. That's the HTTP specification. Because HTTP is stateless, there is no "base path" state associated with a given host.

Relative requests exist in a browser-type environment, but that's only because the browser pre-processes the request and, if it's not absolute, the browser adds on a default base path to make it absolute. All requests coming from a browser to a server are actually absolute URLs.

You could write your own function that did the same thing the browser does. You set a base path on it and then you direct all your requests through this function that checks the URL sent for the request and if it's not a fully qualified path, then the base path is added to it.

We could help you more specifically if you showed a real code example of the problem you're trying to solve. If it's one of dev or production environment, then you would typically establish some variables at startup base don which type of environment you're running in and then just have your code use those variables whenever it forms a request.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Relative was the wrong word to use. I fix it in the title, but forgot to fix it in the body. I dislike having to specify the hostname. I added a code example. Rationally I suppose I would need to use a method from Express to possibly have it know the hostname it is listening as. I could use constants, but then I would need to correctly write my host in some file, which allows user error. It is accessible through req I believe, but I don't want to have to pass it through a million function calls. – Sophie McCarrell Oct 09 '16 at 11:48
  • 1
    @JasonMcCarrell - So, now you're asking why a host name is needed when making an http request? Seriously? If you want to make your own wrapper around some function that makes an http request, that defaults to a particular host, you can certainly do that, but it's not built into the node.js http library. And, usually, you don't need to make an actual HTTP request to your own code. Just expose the functionality you want in a function and make a direct function call. There's usually no reason to go through HTTP to call your own code. – jfriend00 Oct 09 '16 at 13:54
  • call a proxy url? You couldn't think of one reason? – Sophie McCarrell Oct 14 '16 at 04:50
  • It seems having high standards and wanting consistency in isomorphic libraries is too much to ask today. I'm going to close this as it's obviously not productive. FYI I just used a constant. The client side doesn't mind fully qualified URLs even if it doesn't need them. EDIT: nm not going to close it, someone else may come across this and it already has an answer. It's not like it hurts anything to exist. – Sophie McCarrell Oct 14 '16 at 04:52
  • 1
    @JasonMcCarrell - The thing is there is no base URL or hostname for the `HTTP` library or a node.js app. It's just a utility library and a single node.js file could contain multiple servers for multiple hostnames even. Plus, even a server doesn't necessarily know its own hostname. It's just listening for requests coming inbound to this server, no matter how they got to this IP. You could make your own wrapper object for `http` that you set a default hostname on and they you could wrap each method that takes a hostname and add the default hostname if one did not already exist. – jfriend00 Oct 14 '16 at 05:09
  • @JasonMcCarrell - My comment above is that what you seem to think is a common issue is not. I'm not saying there's no utility for it, but it's not something people do often, thus it isn't built in. And, it's not particularly hard to make your own functionality to do this. – jfriend00 Oct 14 '16 at 05:10