简体   繁体   中英

I got the error : Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

I'm trying to register a service worker to a html page and I get the error from title. I'm working on linux,I have the right to write,delete,modify and so on,(I'm on # ). I'm on a path like :

/var/www/a/b/c/d/e/f/project

And here I have a node.js server(index.js) , the html page(index.html) and the service worker(ServiceWorker.js)

The node.js server looks like:

const https = require('https');
const fs = require('fs');
var path = require('path');
const express = require('express');
const app = express();
const router = express.Router();
const pool = require('./mysqldb.js');
const pathView = __dirname + "/views/";
const IMGPath = "/public";
var bodyParser = require("body-parser");
const listenPort = 8010;

// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// Process application/json
app.use(bodyParser.json());

app.set('view engine', 'ejs');

app.use(express.static('public'));
app.use('/public', express.static('public'));

// This route will be used to print the type of HTTP request the particular 
Route is referring to
router.use(function (req, res, next) {
next();
});

app.engine('html', require('ejs').renderFile);

app.get('/index.html',function(req,res){    
 res.render('/var/www/a/b/c/d/e/f/project/index.html');
});
app.use( "/", router);

// Not found
app.use("*",function(req,res){
res.setHeader('Content-Type', 'text/html');
res.status(404).send('Page introuvable !');
});

// Run server
app.listen(listenPort, function () {
console.log( listenPort )
});

//HTTPS
https.createServer(options, app).listen(8000);

And the .html file looks like:

<html>
<body>
<script>

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('ServiceWorker.js', {
    scope: './'
 }).then(function (registration) {
    var serviceWorker;
    if (registration.installing) {
        serviceWorker = registration.installing;
        document.querySelector('#kind').textContent = 'installing';
    } else if (registration.waiting) {
        serviceWorker = registration.waiting;
        document.querySelector('#kind').textContent = 'waiting';
    } else if (registration.active) {
        serviceWorker = registration.active;
        document.querySelector('#kind').textContent = 'active';
    }
    if (serviceWorker) {
        // logState(serviceWorker.state);
        serviceWorker.addEventListener('statechange', function (e) {
            // logState(e.target.state);
        });
    }
}).catch (function (error) {
    // Something went wrong during registration. The service-worker.js file
    // might be unavailable or contain a syntax error.
    console.log('Service Worker registration error : ' , error);
});
} else {
    console.log('Please update your brower!');
// The current browser doesn't support service workers.
}

</script>

</body>
</html>

I run the index.js node( command : "node index.js") Everything is fine,the page is loading(I mean the script from the page) and i get the following error:

Service Worker registration error :  TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

And I don't know what to do.I basically have the ServiceWorker.js,index.html and index.js in the same folder.I try to run them but somehow the path is wrong... Can someone help me?

Include this in your app.js and put your service worker in your public directory/folder.

app.get("/ServiceWorker.js", (req, res) => {
  res.sendFile(path.resolve(__dirname, "public", "ServiceWorker.js"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM