0

Hi i am following peepcode nodejs screencast, now i have an issues of rendering the login form. My code are as follow:

app.js

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


  , http = require('http')
  , path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

require('./apps/authentication/routes');

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

and my i have a routes within authentication folder. The code as follow:

routes.coffee

routes = (app) ->

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

module.exports = routes

The coffee script indentation all works fine, but i have an error when i navigate localhost:3000/login on browser. The error it display are Cannot GET /login. Where am i wrong?

4

1 回答 1

1

在 app.js 中,更改这一行:

require('./apps/authentication/routes');

对此:

require('./apps/authentication/routes')(app);

正在发生的事情是,在 routes.coffee 中,您正在导出一个函数,该函数采用单个参数“app”,然后在您的 app 对象上设置路由。您需要将其称为传递 app 作为参数。

于 2013-05-28T06:12:41.090 回答