[英]NodeJS server failed to load JavaScript modules
SO citizens. SO 公民。 I'm using Node.js with Express.js for the server side of web application.
我在 Web 应用程序的服务器端使用 Node.js 和 Express.js。 And I often encounter problem when webserver is trying to bind references from the source code.
当网络服务器尝试从源代码绑定引用时,我经常遇到问题。 The structure of my app is:
我的应用程序的结构是:
src/
- static/
- calendar.js
- client.js
- planner.js
- index.html
- utils/
- planListItem.js
- selectedDates.js
- index.js
- server.js
In index.js在 index.js 中
import app from "./server.js"
In server.js在 server.js 中
I register an endpoint "/" with callback function that sends back an html
file via res.sendFile()
function passing in absolute path to index.html
.我使用回调函数注册了一个端点“/”,该回调函数通过
res.sendFile()
函数将绝对路径传递给index.html
发送回一个html
文件。
Inside index.html在 index.html 里面
<html>
<head>...</head>
<body>
...
<script type="module" src="./client.js"></script>
</body>
</html>
Server successfully load scripts so far.到目前为止,服务器已成功加载脚本。 But then
但是之后
client.js客户端.js
import { initCalendar } from "./calendar.js"
calendar.js日历.js
import Planner from "./planner.js";
import selectedDays from "../utils/selectedDates"
planner.js规划器.js
import {PlanListItem} from "./../utils/planListItem.js";
Enough with the structure:)结构足够了:)
The problem is, I suppose, in how Express is routing static files, because source code reference were checked 1000 times and are correct.我想问题在于Express如何路由静态文件,因为源代码参考被检查了 1000 次并且是正确的。
To defend myself from sully suggestions, I added the following script inside server.js beforehand app.use(express.static("src/static"))
.为了保护自己免受卑鄙的建议,我事先在server.js中添加了以下脚本
app.use(express.static("src/static"))
。 Which was supposed to help serving scripts.这应该有助于提供脚本。 But unfortunately browser responds
但不幸的是浏览器响应
It's obvious that Node can't find selectedDates.js
and planListItem.js
in utils/.很明显,Node 在 utils/ 中找不到
selectedDates.js
和planListItem.js
。 What should I do to make Node find this files???我该怎么做才能让 Node 找到这个文件???
You need to serve utils
, too.你也需要服务
utils
。 You can add你可以加
app.use('/utils', express.static("src/utils"))
after后
app.use(express.static("src/static"))
All paths inside the HTML document are evaluated on the browser. HTML 文档中的所有路径都在浏览器上进行评估。 The browser sends a request for each import.
浏览器为每次导入发送一个请求。 You server has to handle these requests.
您的服务器必须处理这些请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.