R
R
rumkin2011-07-23 22:18:44
Node.js
rumkin, 2011-07-23 22:18:44

Node.js, Express and 404s?

Problem:
Using Express to mirror my page for errors.
Here's what I tried:

var express = require('express');

var app = module.exports = express.createServer();
var util = require('util');
// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});


// Routes
app.get('/', function(req, res){
  res.render('index', {
    title: 'Express'
  });
});

app.listen(3000);
app.error(function(err, req, res, next){
    console.log('Error!');
    res.render('404.jade', {
        title: '404 — Page maybe not found'
      });
    //next(err);
});

I scoured the Express'a website , but for some reason the indicated examples do not work out.
wtf?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pomeo, 2011-07-24
@rumkin

yeah, it didn’t work either, I tried examples from the docks and looked here github.com/visionmedia/express/tree/master/examples .
I just made a page app.get('/404',…, in the nginx settings I wrote that I have 404 at this address, and in the express code, because I have to describe everything, otherwise it will fall, I just do res.redirect('/404 ')

A
Anton Shevchuk, 2011-09-02
@AntonShevchuk

Works from this example: github.com/visionmedia/express/blob/master/examples/error-pages/app.js

P
Postgoamodernist, 2015-05-16
@Postgoamodernist

app.get('/*', function(req, res){
res.status(404);
res.render('404', {
title: 'Express'
});
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question