如何在 Express 中使用转译的 ES6 代码实例化对象?

How do I instantiate an object with transpiled ES6 code in Express?

我确信我遗漏了一些非常简单的东西,但我无法使用我的节点代码创建对象。我正在使用 webpack 和 Babel 6 为我转译。尝试按照以下说明学习:http://blog.jeffdouglas.com/2015/05/06/start-writing-es6-javascript-in-your-node-js-apps-today/

这是我的 ES6 class:

export default class ProcessData {
    constructor() {
        let output = "Module: 'Process Data' created.";
        console.log(output);
    }
}

这是我尝试使用它的路线:

var express = require('express');
var router = express.Router();
var ProcessData = require('../public/node/bundle.node.js').default;

var processor = new ProcessData();

router.get('/', function (req, res, next) {
    res.render('index', {});
});

router.post('/results', function (req, res) {
    res.render('results', req.body);
});

module.exports = router;

这是转译后的文件:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;

/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            exports: {},
/******/            id: moduleId,
/******/            loaded: false
/******/        };

/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/        // Flag the module as loaded
/******/        module.loaded = true;

/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }


/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;

/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;

/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";

/******/    // Load entry module and return exports
/******/    return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {

    "use strict";

    var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

    Object.defineProperty(exports, "__esModule", {
        value: true
    });

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var ProcessData = (function () {
        function ProcessData() {
            _classCallCheck(this, ProcessData);

            var output = "Module: 'Process Data' created.";
            console.log(output);
        }

        return ProcessData;
    })();

    exports.default = ProcessData;

/***/ }
/******/ ]);

这是我不断收到的错误(新的 'n' 下有一个插入符号):

var 处理器 = new ProcessData();

类型错误:ProcessData 不是函数

你真的不需要 default 或 babel。

var ProcessData = require('path/to/es6-class.js');

当您不确定从 require 中得到什么时,请在其周围加上 console.log

注意 - 你可能需要更深入地研究 es6 类 如果你正在使用最新的服务器端做东西,你可以跳过整个 babel 步骤节点 js lts.

如果您仍然认为需要 babel,则需要使用 https://www.npmjs.com/package/babel-plugin-add-module-exports 来获得 export

的预期行为