title: electron js - serving html
body: <p>I am trying to serve another <code>html</code> file so that browser can access it from <code>electronjs</code>. this is file directory:</p>
  
  <pre><code>app folder:
  - index.js // this is the init file for electron.
  - index.html // main interface.
  - ip.js // &lt;-- included in index.html, codes for serving the static html to browser.
  - package.json
  -- node_modules
  -- pad: // &lt;-- this is the root folder I want to access via browser
      - index.html // &lt;-- browser will get to see this page.
      - pad_index.js
  </code></pre>
  
  <p><code>index.js</code> will have basic <code>electron js</code> stuffs.
  'index.html' will import and run <code>ip.js</code>, which contain codes to serve <code>./pad/index.html</code> to browser.</p>
  
  <pre><code>ip.js:
  var os = require("os");
  
  const http = require("http");
  var path = require("path");
  var url = require("url");
  var fs = require("fs");
  
  const port = 3000;
  
  var staticBasePath = "./pad/";
  
  var staticServe = function(req, res) {
    var resolvedBase = path.resolve(staticBasePath);
    var safeSuffix = path.normalize(req.url).replace(/^(\.\.[\/\\])+/, "");
    var fileLoc = path.join(resolvedBase, safeSuffix);
  
    var stream = fs.createReadStream(fileLoc);
  
    stream.on("error", function(error) {
      res.writeHead(404, "Not Found");
      res.write("404: File Not Found!");
      res.end();
    });
    res.statusCode = 200;
    stream.pipe(res);
  };
  
  var httpServer = http.createServer(staticServe);
  
  httpServer.listen(port,  () =&gt; {
    console.log(`Server running at http://${hostname}:${port}/`);
  });
  </code></pre>
  
  <p>When I test it with <code>electron .</code>, everything works. I can go to browser and type <code>http://localhost:3000/index.html</code> and it will show the content from <code>./pad/index.html</code>.</p>
  
  <p>but after I use <code>electron builder</code> to build the app, then run the built app, I get <code>404: file not found!</code> in the browser. I am sure I missed out something very important but not sure what. </p>
  
  <p>How can I resolve this?</p>