Using NodeJS and ExpressJS to host a static Progressive Web App (PWA)
<p>I am trying to host a static Progressive Web App using ExpressJS, but since I'm fairly new to web dev I'm having some trouble.</p>

<p>I have found a number of great articles on how to use ExpressJS for routing (see links below), but none of them have helped me resolve my problem. These tutorials are quite basic and I can't find any tutorials which are more advanced - if you guys know of any please reply to this thread and link them!</p>

<pre><code>https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
https://stormpath.com/blog/build-nodejs-express-stormpath-app
https://zellwk.com/blog/crud-express-mongodb/
https://www.codementor.io/nodejs/tutorial/build-website-from-scratch-using-expressjs-and-bootstrap
</code></pre>

<p>My folder structure looks like this:</p>

<pre><code>node_modules
package.json
server.js 
index.html
sw.js (service worker)
public/
    js/
        index.js
    style/
        index.css
</code></pre>

<p>and my server.js looks like this:</p>

<pre><code>var express = require('express');
var app = express();

// Initialize static content
app.use(express.static('public'));

app.get('/index.html', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});
app.listen(8887);
</code></pre>

<p>In my index.html I am loading my public/style/index.css stylesheet and public/js/index.js javascript like this:</p>

<pre><code>&lt;link rel="stylesheet" href="public/style/index.css"&gt;
&lt;script src="public/js/index.js"&gt;&lt;/script&gt;
</code></pre>

<p>When I host my app using the command 'node server.js' and I navigate to <a href="http://localhost:8887/index.html" rel="nofollow">http://localhost:8887/index.html</a>, then I get served the correct index.html file but without the index.css file.</p>

<p>Here are my problems:</p>

<ol>
<li>My style sheets aren't being served correctly (index.html renders
    without any css)  </li>
<li>The browser is not picking up the service worker (sw.js)</li>
</ol>

<hr>

<p>Edit: Moving sw.js to the public/ folder causes an error when the service worker loads: GET <a href="http://localhost:8887/sw.js" rel="nofollow">http://localhost:8887/sw.js</a> net::ERR_INVALID_RESPONSE.</p>

<p>Edit 2: More service worker errors: <a href="https://i.stack.imgur.com/DB4iN.png" rel="nofollow"><img src="https://i.stack.imgur.com/DB4iN.png" alt="More service worker errors"></a></p>