martyjs / marty-express

express middleware for building isomorphic applications with Marty & react-router
http://martyjs.org
MIT License
11 stars 12 forks source link

I get `TypeError: undefined is not a function` when providing my Application. #14

Open SudoPlz opened 9 years ago

SudoPlz commented 9 years ago

Hello, this is my express.js file:

'use strict';
    var express = require('express');
    var path = require('path');
    var logger = require('morgan');
    var bodyParser = require('body-parser');
    var configFile = require('../config.json');
    var webpack = require('webpack');
    var WebpackDevServer = require('webpack-dev-server');
    var proxy = require('proxy-middleware');
    var url = require('url');
    var webpackConfig = require('../../webpack.config');
    var morgan = require('morgan');
    var bodyParser = require('body-parser');

    var isProduction = process.env.NODE_ENV === 'production';
    //var publicPath = path.join(__dirname, '..','..', 'assets');

    // -------- my proxy----------------------
    var app = express();
    var server = require('http').Server(app);
    // proxy the request for static assets

    // now requests to '/assets' are proxied to 'http://localhost:8081/assets'
    app.use('/assets', proxy(url.parse('http://localhost:8081/assets')));

    app.use(function logErrors(err, req, res, next) {
            console.error(err.stack);
            next(err);
        }
    );

    //css folder
    app.set('styles', path.join(__dirname, '..', 'styles'));

    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs');

    var PORT = (isProduction ? process.env.PORT: configFile.DEV_PORT);
    app.set('port', process.env.PORT || configFile.DEV_PORT);

    app.use(morgan('dev'));
    app.use(bodyParser.json());
    app.use(require('marty-express')({
        routes: require('../routes'),
        application: require('../application'),
        rendered: function (result) {
            console.log('Rendered ' + result.req.url);
        }

    }));

    if(!isProduction){
        //----- my-webpack-dev-server------------------
        var webpackServer = new WebpackDevServer(webpack(webpackConfig), {
            //contentBase: __dirname,
            hot: false,
            inline: false,
            quiet: false,
            noInfo: false,
            headers: { 'Access-Control-Allow-Origin': '*' },
            publicPath: '/assets/',

            stats: { colors: true }
        });

        //run webpack hot reload server
        webpackServer.listen(8081, 'localhost', function (err) {
            if(err) {
                return console.log(err);
            }
        });
        server.listen(express.get('port'), function() {
            console.info('Express server started at http://localhost:' + express.get('port'));
        });
    }

    module.exports = app;

this is my application.js file

import Marty from 'marty';

function requireFromContext(context, callback) {
    // Iterate through all js files in context folder
    context.keys().forEach(key => {
        if (!/\.js/.test(key)) {
            callback(key);
        }
    });
}

class Application extends Marty.Application {
    registerDependencies() {
        let context = require.context('./', true, /(actions|queries|sources|stores)/);

        requireFromContext(context, key => {
            // NOTE: id could potentially clash if files are named the same.
            let id = key.substr(key.lastIndexOf('/') + 1);
            console.log('Registering marty assets: ', id);
            this.register(id, context(key));
        });
    }

    registerHooks() {
        let context = require.context('./', true, /hooks/);

        requireFromContext(context, key => {
            console.log('registering hook', key);
            context(key); // Run
        });
    }

    constructor(options) {
        super(options);
        console.group('setup');

        this.registerDependencies();
        this.registerHooks();
        this.router = require('./router');
        this.loginActionCreators.attemptReAuth();
        console.groupEnd();
    }
}
module.exports = Application;

For some very strange reason I keep getting this error:

TypeError: undefined is not a function
   at new Application (/Users/Dynopia/Development/lala_Frontend/app/application.js:36:17)
   at Function.<anonymous> (/Users/Dynopia/Development/lala_Frontend/node_modules/marty-express/index.js:73:17)
   at Function.dispatchHandler (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:381:22)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:349:29
   at /Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/Transition.js:69:9
   at /Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/Transition.js:69:9
   at Function.Transition.to (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/Transition.js:72:15)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:348:22
   at Function.Transition.from (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/Transition.js:51:15)
   at Function.dispatch (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:345:20)
   at Function.refresh (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:396:16)
   at Function.run (/Users/Dynopia/Development/lala_Frontend/node_modules/react-router/lib/createRouter.js:392:16)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/marty-express/index.js:72:12
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at jsonParser (/Users/Dynopia/Development/lala_Frontend/node_modules/body-parser/lib/types/json.js:100:40)
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at logger (/Users/Dynopia/Development/lala_Frontend/node_modules/morgan/index.js:144:5)
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:91:12)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at expressInit (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/middleware/init.js:33:5)
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at query (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/middleware/query.js:38:5)
   at Layer.handle [as handle_request] (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:312:13)
   at /Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:330:12)
   at next (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:271:10)
   at Function.handle (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/router/index.js:176:3)
   at EventEmitter.handle (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/application.js:173:10)
   at Server.app (/Users/Dynopia/Development/lala_Frontend/node_modules/express/lib/express.js:38:9)
   at Server.emit (events.js:110:17)
   at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
   at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
   at Socket.socketOnData (_http_server.js:343:22)
   at Socket.emit (events.js:107:17)
   at readableAddChunk (_stream_readable.js:163:16)
   at Socket.Readable.push (_stream_readable.js:126:10)
   at TCP.onread (net.js:538:20)

Some help please?