Javascript 我收到错误:注册ServiceWorker失败:获取脚本时收到错误的HTTP响应代码(404)

Javascript 我收到错误:注册ServiceWorker失败:获取脚本时收到错误的HTTP响应代码(404),javascript,html,node.js,service-worker,progressive-web-apps,Javascript,Html,Node.js,Service Worker,Progressive Web Apps,我正在尝试将一个服务人员注册到一个html页面,我从标题中得到了错误。 我在linux上工作,我有权写、删除、修改等等,(我在#)。 我走的是这样一条路: /var/www/a/b/c/d/e/f/project 这里我有一个node.js服务器(index.js)、html页面(index.html)和服务工作者(serviceworer.js) node.js服务器看起来像: const https = require('https'); const fs = require('fs');

我正在尝试将一个服务人员注册到一个html页面,我从标题中得到了错误。 我在linux上工作,我有权写、删除、修改等等,(我在#)。 我走的是这样一条路:

/var/www/a/b/c/d/e/f/project
这里我有一个node.js服务器(index.js)、html页面(index.html)和服务工作者(serviceworer.js)

node.js服务器看起来像:

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);
<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>
.html文件如下所示:

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);
<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>
我不知道该怎么办。我基本上把ServiceWorker.js、index.html和index.js放在同一个文件夹中。我试着运行它们,但不知怎的,路径错了。。。
有人能帮我吗?

将此信息包含在你的app.js中,并将你的服务人员放在你的公用目录/文件夹中

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

使用浏览器开发人员模式来断言您认为正在来回传递的内容确实是。而且,您的服务器设置似乎没有做任何事情来处理
/ServiceWorker.js
路径,这与它为
/index.html
所做的工作相当。感谢大家的帮助,我想我很接近答案。我在node.js上创建了一个/ServiceWorker“页面”,就像我在post code中对index.html所做的一样,我将res.header设置为“html/javascript”并运行,但当我运行它并打开网页时,控制台上显示了以下错误:找不到模块“js”。你知道如何解决它吗?可能是重复的