快速路由与ES6类 [英] Express Routing with ES6 Classes

查看:169
本文介绍了快速路由与ES6类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,以下代码在开发中运行,并且在生产环境中运行时失败,错误 TypeError:Router.use()需要中间件功能,但是有一个Object 。现在我一直尝试过这个大约一百种不同的方式,结果相同。在开发中工作,而不是在prod。

So, the following code works in development and fails when running in a production environment, with the error TypeError: Router.use() requires middleware function but got a Object. I must have tried this about a hundred different ways by now all with the same result. Works in dev, not in prod.

/server/routes.js

'use strict';

export class AppRoutes
{
    constructor(app)
    {
        this.app = app;

        return function initialize(app)
        {
            app.use('/mail', require('./api/mail'));
        }
    }
}

/ server / app.js

var app = express();
import { AppRoutes } from './routes';
let router = new AppRoutes();
router(app);

// start the server here

// Expose app
export default app;

再次,在开发工作时,ES6被折叠并且该应用程序在生产中运行时损坏。感谢您提前就此事发表任何想法。

Again, working in dev, broken when the ES6 is transpiled and the app is run in production. Thanks in advance for any thoughts on the matter.

更新

错误被抛出在$ code> /node_modules/express/lib/router/index.js 的第458行。这是传递给 Router.use()

The error is thrown on line 458 of /node_modules/express/lib/router/index.js. This is what is being passed to Router.use():

{ default: 
   { [Function: router]
     params: {},
     _params: [],
     caseSensitive: undefined,
     mergeParams: undefined,
     strict: undefined,
     stack: [ [Object], [Object] ] 
    } 
}

以下是我认为与错误相关的依赖关系的版本(在prod中与prod相同):

Here are the versions of the dependencies I think are relevant to the error (same in prod as in dev):

节点v0。 12.2

node v0.12.2

从我的 package.json

{
    "express": "^4.13.3",
    "babel-runtime": "^5.8.20",
    "grunt-babel": "^6.0.0"
},
"devDependencies": {
    "babel-core": "^5.8.34",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-stage-0": "^6.1.18"
}


推荐答案

这是因为在6.0中更改了默认导出行为。

This is happened because has changed default export behavior in 6.0.

现在,如果你想得到默认的导出,你应该隐式地做:

Now if you want to get default export, you should implicitly do it:

app.use('/mail', require('./api/mail').default);

您可以在另一个问题

另外,保留所有 babel - * 包在相同的主要版本。 6.0预设将不能与5.8 babel核心工作。

Also, keep all babel-* packages at the same major version. 6.0 presets will not work with 5.8 babel-core.

这篇关于快速路由与ES6类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆