简体   繁体   English

类型错误:无法注册 ServiceWorker:获取脚本时收到错误的 HTTP 响应代码 (404)

[英]TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

I'm trying to add a service worker to a website.I'm using Node.js with Express.js,Vue.js and EJS.^我正在尝试向网站添加 Service Worker。我将 Node.js 与 Express.js、Vue.js 和 EJS 一起使用。^

My node server (index.js) looks like :我的节点服务器(index.js)看起来像:

const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
const router = express.Router();
const pool = require('./mysqldb.js');
const pathView = __dirname + "/views/";
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'));


router.use(function (req, res, next) {
  next();
});

// home
router.get('/', function (req, res) {
   res.render( 'home.ejs', { msg:" > "  });
});

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('Example app listening on port ' + listenPort )
});

And the home.ejs file looks like: home.ejs 文件如下所示:

<!DOCTYPE html>
<html lang="en">

<body>

<script>
   if ('serviceWorker' in navigator) {
          navigator.serviceWorker.register('ServiceWorker.js').then(function(registration)     {
  console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }).catch(function(err) {
      //registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
  }else {
    console.log('No service-worker on this browser');}
</script>

</body>
    </html>

I have to mention that I'm testing it on https.我不得不提一下,我正在 https 上测试它。 I don't think the content of ServiceWorker.js is that importat,but if you would need it to help me I can post it too.我不认为 ServiceWorker.js 的内容那么重要,但是如果您需要它来帮助我,我也可以发布它。 The server files scheme would look like:服务器文件方案如下所示:

  index.js
  mysqldb.js
  public //(folder)
  views /*(folder)*/ : home.ejs , ServiceWorker.js

And when I open the website on https I get the following error in web console:当我在 https 打开网站时,我在 Web 控制台中收到以下错误:

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

ServiceWorker.js is in your views folder. ServiceWorker.js在您的views文件夹中。 That is for templates that get rendered by express as a page.这适用于通过 express 作为页面呈现的模板。 Move it to public so that it gets served like a static file.将其移动到public以便它像静态文件一样提供服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误消息:无法注册ServiceWorker:获取脚本时收到错误的HTTP响应代码(404) - I got the error : Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script FirebaseError browserErrorMessage:“无法注册 ServiceWorker:ServiceWorker 脚本评估失败” - FirebaseError browserErrorMessage: "Failed to register a ServiceWorker: ServiceWorker script evaluation failed" 尝试注册 serviceWorker 时出现 404 错误 - 404 error when trying to register serviceWorker Angular 2 - 找不到服务工作者 - 错误的HTTP响应代码(404) - Angular 2 - Service worker not found - A bad HTTP response code (404) 注册 ServiceWorker 失败:脚本的 MIME 类型不受支持 - reactjs - Failed to register a ServiceWorker: The script has an unsupported MIME type - reactjs AbortError:无法注册ServiceWorker:无法启动ServiceWorker - AbortError: Failed to register a ServiceWorker: ServiceWorker cannot be started 为 scope angular 注册 ServiceWorker 失败 - Failed to register a ServiceWorker for scope angular JavaScript / Firefox:当未使用任何称为ServiceWorker的内容时,“无法注册/更新ServiceWorker” - JavaScript/Firefox: “Failed to register/update ServiceWorker”, when nothing called ServiceWorker is in use ServiceWorker 脚本评估失败 - ServiceWorker script evaluation failed MSW:无法使用脚本为范围注册 ServiceWorker 该脚本具有不受支持的 MIME 类型(&#39;text/html&#39;) - MSW: Failed to register a ServiceWorker for scope with script The script has an unsupported MIME type ('text/html')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM