3

I bought the latest nodejs peepcode tutorial and followed it, however I can't get past the initial step.

I'm getting frustrated after spending several hours to find out where I got an error since debugging nodejs is a riddle for me.

app structure looks like this:

example 
  |__public
  |__views
  |__assets 
  |__apps <- instead of routes
  server.js
  package.json

Here is my simple code:

server.js

/**
 * Module dependencies.
 */
require('coffee-script');

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

// 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
require('./apps/authentication/routes')(app);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

/apps/authentication/routes.coffee:

routes = (app) ->

    app.get '/login', (req, res) ->
        res.render "views/login",
            title: 'Login'
            stylesheet: 'login'

module.exports = routes

apps/authentication/views/login.jade template:

form(action='/sessions', method='post')
  label
    | Username
    input(type='text', name='user')
  label
    | Password
    input(type='password', name='password')
  input(type='submit', name='Submit')

nothing fancy, i got a stylesheet file and login.css in public/stylesheet/login.css instead of a login template from authentication/routes.coffe when browsing http://localhost:3000/

Cannot GET /

no any other error message from node either:

Express server listening on port 3000 in development mode

I can't figure out where the problem is and this is really frustrating. Probably some dumb typo somewhere but I can't figure this out :(

4

2 回答 2

2

You do not have a route configured for the root '/'. Navigating to http://localhost:3000/login should return your login view as specified by the route to the resource '/login'. You need to add something along the lines of:

app.get '/', (req, res) ->
  #if not logged-in then send to /login else
  res.render('/views/authenticated', 'Home', 'index')

For more details on routing see http://expressjs.com/guide.html#routing.

于 2012-04-11T14:42:51.360 回答
1

It looks like everything is working as intended. The problem is that you haven't defined a route that matches the request GET /. You've only defined a route matching GET /login in your routes.coffee; also, GET /anythinginyourpublicdir will work thanks to the express.static middleware.

于 2012-04-11T14:33:21.217 回答