Javascript Express中的脚本标记

Javascript Express中的脚本标记,javascript,express,Javascript,Express,我有一个index.html,它有几个脚本标记,但它们都返回404个错误,我不知道如何修复它。目前,它们位于顶级目录中,并作为顶级目录引用。e、 g. 我尝试使用require('./file.js')

我有一个index.html,它有几个脚本标记,但它们都返回404个错误,我不知道如何修复它。目前,它们位于顶级目录中,并作为顶级目录引用。e、 g.


我尝试使用
require('./file.js')指定给的
根路径是Express开始匹配磁盘上文件的目录

app.use(express.static(path.join(__dirname, 'static')));

该路径也不会是URL的一部分。它与的组合方式类似于:

var rootPath = 'orbit'; // from `express.static('orbit')`

console.log(path.join(rootPath, req.path));
// 'orbit/orbit/util.js'
请注意,与注释中的路径相比,
orbit
出现两次,并且缺少
static

/static/orbit/util.js
或者,使用上面建议的路径:

var rootPath = path.join(__dirname, 'static');

console.log(path.join(rootPath, req.path));
// "/path/to/your/application/static/orbit/util.js"

// assuming `__dirname` is `/path/to/your/application/`

您是如何配置Express应用程序来提供
index.html
util.js
?您能否直接请求
util.js
作为浏览器中的地址?如果是,它的URL和页面的URL如何比较?它们有共同的目录吗?我有
app.use(express.static('orbit'))
和所有js文件都位于
/static/orbit/
中,但到达的地址(例如localhost:3000/orbit/util.js)返回'Cannot GET/orbit/util.js'