I also had this error when using the express server. It turns out that the problem is with the server setup itself, and not with the registration code of the service worker. I told my express application to get index.html as the default root using:
app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/index.html')); });
However, I did not say that I can specify the location of other files that I would like to use. At this point, my only other file was the worker file that was in the root of the directory, so I fixed the problem by adding this line to the server file:
app.use(express.static(__dirname + '/'));
To debug the question of whether your problem is related to the server itself, you can download the web server for Chrome and point it to the root directory of your application. Verify the server is starting up and click the URL of the web server. If the registration of your service worker is successful, you will find out that the problem with setting up your express server is not your registration code for the service worker.
source share