javascript - 将express-babelify-middleware 与 FeathersJS 结合使用

标签 javascript node.js express feathersjs

我正在尝试使用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/

相关文章:

javascript - 单击浏览器后退按钮时禁用页面上的提交按钮

javascript - fillRect 导致整个屏幕改变颜色

node.js - 当我运行 npm start 创建 React 应用程序时缺少脚本

node.js - 如何在 Express POST 路由中使用 Mongoose 保存方法

javascript - 表达正则表达式字符串表示法

node.js - 为生产部署 Node.js 应用程序

javascript - 使用用户帐户登录 MongoDB

javascript - 使用 Express 提供动态生成的图像或保存的图像

javascript - Cookie 未在 node.js 中设置

javascript - 在 map 循环中计数 jsx