我正在尝试使用express-babelify-middleware
使用 FeathersJS 并在浏览器控制台中显示错误:
引用错误:main_run 未定义
我认为这意味着 babelify 不起作用或者我错误地使用了它,因为 main_run 位于我的 html 文件中 src 的全局命名空间中。
这是我使用羽毛生成的结构进行的设置:
public/index.html:
<!DOCTYPE html>
<html>
<head>
<title>babelify test</title>
<script src="main.js"></script>
<script>
main_run()
</script>
</head><body>
<p>Testing feathers with babelify</p>
</body></html>
public/main.js
const external_module = require('./test')
function main_run(){
external_module()
}
public/test.js
module.exports = function(){
console.log("Hello world for an external module")
}
src/app.js 的用途:
...
const babelify = require('express-babelify-middleware')
...
app.use(compress())
.options('*', cors())
.use(cors())
//the line that is not working:
.use('/main.js', babelify( path.join(app.get('public'), 'main.js') ))
.use(favicon( path.join(app.get('public'), 'favicon.ico') ))
.use('/', serveStatic( app.get('public') ))
当我访问 localhost:3030/main.js 时,我可以看到该文件,但这些函数看起来位于它们自己的函数中,所以我不知道如何进入该函数。
最佳答案
愚蠢的问题,无法访问调用它的 html 文件中的浏览器化代码。因此 public/index.html 无法访问 main_run ,除非它附加到 window 对象。有一个类似的问题
here.
除此之外,我的代码运行完美。
在 main.js 的底部放置以下代码:
window.main_run = main_run
然后在index.html中将main_run()行替换为:
window.main_run()
这会将 test.js 的内容写入控制台。
关于javascript - 将express-babelify-middleware 与 FeathersJS 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224730/