我在那里,
我已经找到了一个似乎是我的问题的答案,我已经完成了可以在与我的问题相关的答案中找到的实现,但它们没有用。
dev机器:windows 10亲 安装包:
"express": "^4.16.3",
"request": "^2.87.0",
"serve-static": "^1.13.2",
dir文件夹
|app
| |- web
| | |-pages
| | | - <app domain folder>
| | | | - index.html
| | | |- routeHandler.js
| | |-static
| | | - js
| | | | - appClient.ls
|infrascruture
| - web
| | -builder.js
|index.js
在buider.js上我有所有快速样板代码来启动服务器,包括静态中间件: 像这样:
var serveStatic = require("serve-static");
module.exports = (function(express)
{
module = {};
module.webApp = express();
module.addAppComponents = function() {}
module.addStaticMiddleware = function()
{
module.webApp.use('static', serveStatic(path.join(__dirname, "/app/web/static/js/")));
}
return module;
});
在应用程序根目录上的index.js上我从该模块的一个实例调用addStaticMiddleware()。
app域文件夹中的index.html提供给正在运行且html为:
的路由<!DOCTYPE html>
<html>
<script src="/static/clientApp.js" ></script>
<body onload="loaded()">
<h1> Endpoints</h1>
<h2> These are the endpoints available to subscribe</h2>
</body>
</html>
并且与之前注册的路由关联的index.html正在客户端上正确呈现,但javascript未被提取:
浏览器正在执行的请求是:
// fetching the index.html
request: GET url: localhost:8080/pages/endpoints/{}
headers: {"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9"}
body: "{}"
// fetching the javascript but not working
request: GET url: localhost:8080/static/clientApp.js{}
headers: {"host":"localhost:8080","connection":"keep-alive","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","accept":"*/*","referer":"http://localhost:8080/pages/endpoints/","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9"}
任何想法?
提前知道了!答案 0 :(得分:0)
使用
module.webApp.use('static', serveStatic('../../app/web/static/js/')));
而不是
module.webApp.use('static', serveStatic(path.join(__dirname, "/app/web/static/js/")));