javascript - Node.js 错误,包括 js 文件 - 找不到文件

标签 javascript html node.js

我正在运行一个 Node.js 服务器。在 Node 中,我需要并使用

运行该模块
var h = require('h.js');
h.hello();

这会将 Hello World 打印到控制台。但是,我也希望能够从浏览器中的页面(entrypage.html)运行 h.js 中的代码。我尝试通过

导入它
<script type="text/javascript" src="/node_modules/h.js"></script>

但是,在本地主机上运行时会出现 404 错误。

GET http://localhost:8080/node_modules/h.js 

如何在 HTML 页面上访问此 javascript 文件?

我的文件结构的根目录为 html/、js/、node_modules/和 server.js。 HTML页面在html/里面,js文件在node_modules/

编辑:我为此使用 MEAN 堆栈 - MongoDB、Express.js、Angular.js 和 Node.js。

最佳答案

应该可以使用 express.static 提供文件(示例:app.use('/node_modules/', express.static(__dirname + '/node_modules/') );),但我强烈建议不要在前端提供 node_modules 和后端特定文件!

关于javascript - Node.js 错误,包括 js 文件 - 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994116/

相关文章:

javascript - 为什么这个 fs 循环读取文件名而不是它们的内容?

javascript - 使用字段名称创建新的 Javascript 数组

javascript - ASP :TextBox not cooperating with javascript

html - 编码横向模式 : Is there an inspector for iOS simulator?

node.js - 如何从外部应用程序写入 Meteor 数据库?

javascript - 检查 iframe 是否准备好写入

javascript - 在javascript中访问路径(concrete5)

javascript - 在 JavaScript 中对 CheckBox 选中/取消选中执行验证

node.js - 类型为 "string"的预期参数, "stdClass"给定错误

node.js - 如何使用 Node.js 即时创建图像?