Coming from Java world, where the Servlet-based application context path is set based on the WAR file name, I am trying to understand the best practices for defining context path in Node.js.
The Node application in question has no context path defined in the code. The Express code assumes that request to get a story, for example, has URL with path of /story/1. Thus, JavaScript UI code would makes a request to http://host:port/story/1. Likewise, for the user to connect to the main app page, they would go to http://host:port/.
I would like to change the URL the user sees to http://host:port/myapp. The question is how to consistently define "myapp" as the application context. The options I am considering:
- Defining the context in Express.js code.
- Defining the context in Nginx proxy server.
How do I make sure that the user always sees "myapp" in the URL? Do I also need to remap all the internal requests (the ones made by the UI code) to also have '/myapp' context?
Using Nginx seems cleaner since it does not require changing the code. But can this objective be achieved through Nginx configuration alone and if so, how?
Since this is a common problem, there must be a well-defined pattern for solving it.