简体   繁体   English

无法加载样式和执行脚本Express JS

[英]Unable to load style and execute script Express JS

I have my GET request trying to load an html page in my routes.js file using 我有我的GET请求试图使用以下命令在我的route.js文件中加载html页面

  app.get('/api/testresult', (req, res) => {
    res.sendFile('/path/myfile.html');
  });

Also, in my server.js i have the following 另外,在我的server.js中,我有以下内容

const express        = require('express');
const bodyParser     = require('body-parser');
const app            = express();
const path = require('path');

const port = 8000;

app.use('/', express.static(path.join(__dirname, './app/report')));
require('./app/routes')(app, {});
app.listen(port, () => {
  console.log('Server running on ' + port);
});

However when i try to do a GET call, it throws the error on the browser console 但是,当我尝试执行GET调用时,它会在浏览器控制台上引发错误

localhost/:1 Refused to apply style from
'http://localhost:8000/api/testresult/assets/app.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:2 GET http://localhost:8000/api/testresult/assets/app.js 404 (Not Found)
localhost/:1 Refused to execute script from 'http://localhost:8000/api/testresult/assets/app.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

My file structure is 我的文件结构是

MyApp 
app 
 —report 
      —assets 
        —app.css
        —app.js
     —my file.html
 —routes
    —index.js
    —routes.js
node_modules
package.json
server.js

What am I missing here ? 我在这里想念什么?

So, your directory hierarchy shows app.js at /assets/app.js from where you pointed your express.static() middleware. 因此,您的目录层次结构在/assets/app.js从您指向express.static()中间件的位置显示了app.js So, that's the URL that you would need to use for it to serve that file, but you are apparently using a URL with the path /api/testresult/assets/app.js . 因此,这就是您用来为其提供文件的URL,但是您显然使用的URL路径为/api/testresult/assets/app.js Those simply don't match. 那些根本不匹配。 That URL would be looking in: 该URL将在:

app/report/api/testresult/assets/app.js

But, that isn't where the file is (thus you get a 404). 但是,那不是文件所在的位置(因此您得到了404)。

You can fix it a couple ways: 您可以通过以下几种方法修复它:

  1. Change the client side URL to "/assets/app.js" . 将客户端URL更改为"/assets/app.js" I'm guessing you may have specified "assets/app.js" in your client file. 我猜您可能在客户端文件中指定了"assets/app.js" Because this is a relative URL, the browser would then combine that with the path of your web page to construct the URL your server got a request for /api/testresult/assets/app.js . 由于这是一个相对URL,因此浏览器会将其与您的网页路径结合起来,以构造URL,从而使服务器获得对/api/testresult/assets/app.js的请求。
  2. Change the express.static line to include the proper path prefix app.use('/api/testresult', express.static(path.join(__dirname, './app/report'))); 更改express.static行以包含适当的路径前缀app.use('/api/testresult', express.static(path.join(__dirname, './app/report')));

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

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