grunt-express: Configuration with socket.io ignores http bases

I have an weird issue while I am trying to setup my project to use grunt-express, I am currently trying to configure socket.io to work which sort of breaks normal asset routing.

I have two modes which I use to run my server, dev and prod and based on those modes I will serve my files from different locations respectively.

Relevant stuff in Gruntfile.js:

express: {
  options: {
    server: "app.js",
    port: 3000,
    hostname: "localhost"
  },
  dev: {
    options: {
      debug: true,
      bases: "dev"
    }
  },
  prod: {
    options: {
      bases: "public"
    }
  }
}, .. etc ..

I am using this: app.js:

/**
 * Module dependencies
 *
 */
var express = require("express"),
  packageJson = require("./package.json"),
  app = express(),
  path = require('path'),
  httpServer = require("http").createServer(app),
  io = require("socket.io").listen(httpServer);

/**
 * Express
 *
 */
app.configure(function() {

  app.use(express.compress());
  app.set("views", __dirname + "/app/views");
  app.set("view engine", "jade");
  app.use(express.favicon(__dirname + "/public/img/favicon.ico"));
  app.use(express.logger("dev"));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);

  /**
   * 404 page
   *
   */
  app.use(function(req, res) {

    res.status(404).render("404");

  });

});

/**
 * Socket.io
 *
 */
io.configure(function() {

  io.set("log level", 2);

});

/**
 * Routes
 *
 */
require("./app/routes")(app);

/**
 * Export
 *
 */
exports = module.exports = httpServer;

// Adding necessary use() method for returnable httpServer instance
exports.use = function() {
  app.use.apply(app, arguments);
};

With setup like this, if I run my server in dev mode happens the following (bases is ignored, but http://localhost:3000/socket.io/socket.io.js works!:

socket.io.js  - 200 - FOUND
main.css      - 404 - NOT FOUND
require.js    - 404 - NOT FOUND

However, if I change my app.js configure section to add for example dev route manually (same as bases), everything works perfectly:

app.use(express.static(path.join(__dirname, 'dev')));

socket.io.js  - 200 - FOUND
main.css      - 200 - FOUND
require.js    - 200 - FOUND
etc.js        - 200 - FOUND

The reason why I wan't bases to work is because it will be less hacky and those were meant to be used like that, they will work if I remove socket.io stuff completely.

Any hints? thnx!

Answers


Per the server spec in the README, you don't want to use listen() in your app, so that's one issue I see. I also see you're using debug, which was deprecated.


Need Your Help

ASP.NET Persistent Caching ("Lazy loading"-style?)

c# asp.net caching

I'm having some trouble getting my cache to work the way I want.

rrdtool graph without downsampling data?

graph monitoring rrdtool

I'd like to leverage the beautiful (in my opinion) graphing abilities of RRDtool with data stores that don't down sample (influxdb, statsd, etc).