使用NodeJS和ExpressJS托pipe静态渐进式Web应用程序(PWA)
我正在尝试使用ExpressJS来托pipe一个静态的渐进式Web应用程序,但由于我对web dev相当陌生,所以我遇到了一些麻烦。
我发现了许多关于如何使用ExpressJS进行路由的精彩文章(请参阅下面的链接),但没有一篇能够帮助我解决问题。 这些教程是相当基本的,我找不到任何更先进的教程 – 如果你们知道任何请回复此线程并将其链接!
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
我的文件夹结构如下所示:
node_modules package.json server.js index.html sw.js (service worker) public/ js/ index.js style/ index.css
和我的server.js看起来像这样:
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);
在我的index.html我加载我的public / style / index.css样式表和public / js / index.js javascript像这样:
<link rel="stylesheet" href="public/style/index.css"> <script src="public/js/index.js"></script>
当我使用命令“node server.js”托pipe我的应用程序,并导航到http:// localhost:8887 / index.html时 ,我得到了正确的index.html文件,但没有index.css文件。
这是我的问题:
- 我的样式表没有被正确的服务(index.html没有任何CSS呈现)
- 浏览器不拿起服务人员(sw.js)
编辑:将sw.js移动到public /文件夹会导致服务工作负载时出现错误:GET http:// localhost:8887 / sw.js net :: ERR_INVALID_RESPONSE。
编辑2:更多服务人员错误:
我build议像这样registry达静态:
var path = require('path'); app.use(express.static(path.join(__dirname, 'public')));
然后,像这样引用你的脚本:
<link rel="stylesheet" href="/style/index.css"> <script src="/js/index.js"></script>
服务人员在做什么?