Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js / Expess.js is not recognizing static routes

Node.js / Expess.js is not recognizing static routes

Problem

I have the following settings (it's called by app.js) but i can't seem to get Node.js / Express.js to recognize the static routes. The files exist but I'm still getting a 404.

What am I missing here? I've seen a few issues where people had their static declaration before stylus but that's not the case. Besides, even js files that are physically on my box are 404ing.

Thanks!

var fs = require('fs')
  , express = require('express')
  , basicAuth = require('express').basicAuth
  , stylus = require('stylus')
  , csrf = require('express-csrf')
  , gzippo = require('gzippo')
  , canSecurity = require('cansecurity')
  , emessages = require('express-messages')
  , dateformat = require('./lib/dateformat')
  , csauth = require('./cs/auth')
  , cansec;

  cansec = canSecurity.init({
     getUser: csauth.getUser
   , validatePassword: csauth.validatePassword 
   , sessionKey: 'secret'
   , fields: {id: "_id"}
}); 
exports.boot = function(app){
  bootApplication(app);
  bootErrorConfig(app);
};
function bootApplication(app) {
  app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.set('view options', { layout: 'layouts/default' });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({ secret: 'secret' }));
    app.use(express.logger(':method :url :status'));
    app.use(express.favicon());
    app.use(cansec.validate);
    app.use(app.router);
  });
  app.dynamicHelpers({
    request: function(req){
      return req;
    },
    hasMessages: function(req){
      if (!req.session) return false;
      return Object.keys(req.session.flash || {}).length;
    },

    messages: emessages,

    dateformat: function(req, res) {
      return dateformat.strftime;
    },

    csrf: csrf.token
  });


  function compile(str, path) {
    return stylus(str)
      .set('filename', path)
      .set('warn', true)
      .set('compress', true)
      .define('img', stylus.url({ paths: [__dirname + '/public/images'], limit:1000000 }));
  };
  app.use(stylus.middleware({
      debug: true
    , src: __dirname + '/stylus'
    , dest: __dirname + '/public'
    , compile: compile
  }));

  app.use(express.static(__dirname + '/public'));
  app.set('showStackError', false);
  app.configure('development', function(){
    app.set('showStackError', true);
  });
  app.configure('staging', function(){
    //app.use(gzippo.staticGzip(__dirname + '/public', { maxAge: oneYear }));
    //app.enable('view cache');
  });
  app.configure('production', function(){
    //app.use(gzippo.staticGzip(__dirname + '/public', { maxAge: oneYear }));
  });
  app.use(csrf.check());
}
function bootErrorConfig(app) {
  app.use(function(req, res, next){ next(new NotFound(req.url)); });
  function NotFound(path){
    this.name = 'NotFound';
    if (path) {
      Error.call(this, 'Cannot find ' + path);
      this.path = path;
    } else {
      Error.call(this, 'Not Found');
    }
    Error.captureStackTrace(this, arguments.callee);
  }
  NotFound.prototype.__proto__ = Error.prototype;
  app.error(function(err, req, res, next){
    if (err instanceof NotFound){
      console.log(err.stack);
      res.render('404', {
        layout: 'layouts/default',
        status: 404,
        error: err,
        showStack: app.settings.showStackError,
        title: 'Oops! The page you requested desn\'t exist'
      });
    }
    else {
      console.log(err.stack);
      res.render('500', {
        layout: 'layouts/default',
        status: 500,
        error: err,
        showStack: app.settings.showStackError,
        title: 'Oops! Something went wrong!'
      });
    }
  });

  appName = 'test'

 app.get('/', function(req, res){
    res.redirect('/register');
  });

}
Problem courtesy of: chanced

Solution

You sure your URLs are right? They should look like:

http://yourdomain.com/stylesheets/style.css

If your'e doing /public/stylesheets, you're wrong.

Solution courtesy of: Paul

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js / Expess.js is not recognizing static routes

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×