HenrikJoreteg / moonboots

A set of conventions and tools for bundle and serving clientside apps with node.js
159 stars 20 forks source link

Problems with Express 4 applications #34

Closed kokujin closed 9 years ago

kokujin commented 10 years ago

I have been trying to get MoonBoots to work with a fresh scaffolded Express 4 application. Here are the steps I took:

  1. Called "express" in my test folder to generate a fresh Express 4 app.
  2. Called "npm install moonboots --save
  3. Added "public/javascripts/jquery.js" and "public/javascripts/app.js" files
  4. Edited the autogenerated Express 4 app.js to import and configure moonboots, here:
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var Moonboots = require('moonboots');
//console.log('__dirname', __dirname);

var clientApp = new Moonboots({
    main: __dirname + '/public/javascripts/app.js',
    libraries: [
        __dirname + '/public/javascripts/jquery.js'
    ],
    stylesheets: [
        __dirname + '/public/stylesheets/style.css'
    ]
});

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

app.get(clientApp.jsFileName(),
    function (req, res) {
        clientApp.jsSource(function (err, js) {
            res.send(js);
        })
    }
);
app.get('/app*', clientApp.htmlSource());

module.exports = app;

The application crashes with the following error if I try to fire it up

Users/kokujin/Development/Research/moonboots_test/node_modules/express/node_modules/path-to-regexp/index.js:34
    .concat(strict ? '' : '/?')
     ^
TypeError: Cannot call method 'concat' of undefined
    at pathtoRegexp (/Users/kokujin/Development/Research/moonboots_test/node_modules/express/node_modules/path-to-regexp/index.js:34:6)
    at new Layer (/Users/kokujin/Development/Research/moonboots_test/node_modules/express/lib/router/layer.js:21:17)
    at Function.proto.route (/Users/kokujin/Development/Research/moonboots_test/node_modules/express/lib/router/index.js:382:15)
    at Function.app.(anonymous function) [as get] (/Users/kokujin/Development/Research/moonboots_test/node_modules/express/lib/application.js:406:30)
    at Object.<anonymous> (/Users/saina/Development/Research/moonboots_test/app.js:73:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

npm ERR! moonboots_test@0.0.1 start: `node ./bin/www`
npm ERR! Exit status 8

Can someone tell me what I am doing wrong? Thanks

lukekarrys commented 9 years ago

Hey @kokujin, if you are using express I would recommend using the https://github.com/lukekarrys/moonboots-express module. The readme has instructions for how to setup your app. Also the ampersand cli should soon be updated to use this and work with Express 4 (once this PR is merged and published https://github.com/AmpersandJS/ampersand/issues/16).