title: NodeJS Express - Return the JSON file of the URL path that it triggered
body: <p>I trying to write a NodeJS project that read JSON files from the project directory and response back the JSON file data whenever, user trigger a specific URL.</p>
  
  <p>For the Project below, I am able to get the JSON data response when I trigger <code>http://localhost:8081/api/user</code> in web browser.</p>
  
  <p>In order to make this work I have to <strong>explicitly hardcode</strong> a function that response to a specific URL. For example, if I wanted to add 1 more JSON files in the <code>api folder</code> and enable user to access it, I have to <strong>explicitly hardcode</strong> another function just to response to different request.</p>
  
  <pre><code>app.get('/api/newJSONFile', (req, res) =&gt; {
  
         fs.readFile( __dirname +'/api/newJSONFile' +".json", 'utf8', function (err, data) {
             res.send(data);
             res.end( data );
         });
  
      });
  </code></pre>
  
  <p>So, after adding the code above the user will able to access the JSON file through <code>http://localhost:8081/api/newJsonFile</code>. Without the <strong>explicitly hardcode</strong> 
   function above user will not able to get response when launch the <code>http://localhost:8081/api/newJsonFile</code></p>
  
  <p><strong>My Question:</strong></p>
  
  <ol>
  <li><p>Is this the only way to perform the response upon URL request? It seem inefficient, as if I have 100 JSON File then I have to <strong>explicitly hardcode</strong> another 100 functions.</p></li>
  <li><p>What is the proper way to code the implementation?</p></li>
  </ol>
  
  <p>Thank you.</p>
  
  <p><strong>My Project Structure:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ntI4Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ntI4Q.png" alt="enter image description here"></a></p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>const express = require('express');
  const app = express();
  const fs = require("fs");
  
  
  app.get('/api/user', (req, res) =&gt; {
  
     fs.readFile( __dirname +'/api/user' +".json", 'utf8', function (err, data) {
         res.send(data);
         res.end( data );
     });
  
  });
  
  
  var server = app.listen(8081, () =&gt; {
  
    var host = server.address().address
    var port = server.address().port
  
    console.log("Example app listening at http://%s:%s", host, port)
  
  })
  </code></pre>