title: How to define Node.js application context path?
body: <p>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.</p>
  
  <p>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 <a href="http://host:port/story/1" rel="noreferrer">http://host:port/story/1</a>. Likewise, for the user to connect to the main app page, they would go to <a href="http://host:port/" rel="noreferrer">http://host:port/</a>.</p>
  
  <p>I would like to change the URL the user sees to <a href="http://host:port/myapp" rel="noreferrer">http://host:port/myapp</a>. The question is how to consistently define "myapp" as the application context. The options I am considering:</p>
  
  <ul>
  <li>Defining the context in Express.js code.</li>
  <li>Defining the context in Nginx proxy server.</li>
  </ul>
  
  <p>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? </p>
  
  <p>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?  </p>
  
  <p>Since this is a common problem, there must be a well-defined pattern for solving it.  </p>