首页 > 解决方案 > 静态快递不提供简单的html页面

问题描述

敲我的脑袋……不是 Javascript 的专业人士,但通常经验丰富,足以让事情顺利进行。这次不是,我不明白为什么:

节点:8.15.1 Npm:6.13.7

更新:这里的 jslib 只是一个示例名称(实际上它是一个商业 3rd 方库),实际上不是 jslib。

包.json:

{
  "name": "mytest",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {},
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "jslib": "file:../path_to_local_lib/lib/es-modules"
  }
}

server.js:

const express = require('express');
const app = express();

app.use(express.static(__dirname + '/public'));

const port = 8000;
app.listen(port, function() {
    console.log('server listening on port ' + port)
});

在我的 /public 文件夹中,我有一个 index.html 和一个 basegraph.js:

索引.html:

<!DOCTYPE html>
<html lang="de">
<head>
   <link rel="stylesheet" href="../node_modules/lib/lib.css">
</head>
<body>

<div id="graphComponent"></div>

<script type="module" crossorigin="anonymous" src="basegraph.js"></script>
</body>
</html>

和 basegraph.js:

import { GraphComponent } from 'jslib'

const graphComponent = new GraphComponent('#graphComponent');

现在npm install运行没有问题,npm start在端口 8000 上启动服务器,但在我的浏览器中打开它会导致:

拒绝应用来自 ' http://localhost:8000/node_modules/lib/lib.css ' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。

所以我想我对静态快递有问题吗?但我不明白。

第二:

未捕获的类型错误:无法解析模块说明符“jslib”。相对引用必须以“/”、“./”或“../”开头。

那么我在这里做错了什么?:-(

标签: javascriptexpress

解决方案


推荐阅读