使用带有FeathersJS的express-babelify-middleware

时间:2016-12-19 14:26:33

标签: javascript node.js express feathersjs

我正在尝试使用express-babelify-middleware
使用FeathersJS并在浏览器控制台中显示错误:

ReferenceError:未定义main_run

我认为这意味着babelify无效或我使用不正确,因为main_run位于我的html文件中src的全局命名空间中。
这是我使用羽毛生成结构的设置:

公共/ 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>

公共/ main.js

const external_module = require('./test')

function main_run(){
external_module()
}

公共/ test.js

module.exports = function(){
console.log("Hello world for an external module")
}

在src / app.js的.uses中:

...

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我可以看到该文件,但函数看起来是他们自己的函数,所以我不知道如何进入该函数。

1 个答案:

答案 0 :(得分:1)

愚蠢的问题,人们无法访问调用它的html文件中的浏览器化代码。因此public / index.html无法访问main_run,除非它附加到window对象。还有一个类似的问题 here.
除此之外,我的代码完美无缺 在main.js中,将以下代码放在底部:

window.main_run = main_run

然后在index.html中将main_run()行替换为:

window.main_run()

这会将test.js的内容写入控制台。