404重定向在Express Application中无法正常运行

时间:2013-05-05 15:18:01

标签: node.js express coffeescript

我有这个routes.coffee

#### Routes
# We are setting up theese routes:
#
# GET, POST, PUT, DELETE methods are going to the same controller methods - we dont care.
# We are using method names to determine controller actions for clearness.
urls = [
  "map"
  "guide"
]

module.exports = (app) ->
  #   - _/_ -> controllers/index/index method
  app.all "/", (req, res, next) ->
    routeMvc("index", "index", req, res, next)

  for url in urls
    do (url) ->
      app.all "/#{url}", (req, res, next) ->
        res.render "#{url}"
        routeMvc("#{url}", "#{url}", req, res, next)

  #   - _/**:controller**_  -> controllers/***:controller***/index method
  app.all "/:controller", (req, res, next) ->
    routeMvc(req.params.controller, "index", req, res, next)

  #   - _/**:controller**/**:method**_ -> controllers/***:controller***/***:method*** method
  app.all "/:controller/:method", (req, res, next) ->
    routeMvc(req.params.controller, req.params.method, req, res, next)

  #   - _/**:controller**/**:method**/**:id**_ -> controllers/***:controller***/***:method*** method with ***:id*** param passed
  app.all "/:controller/:method/:id", (req, res, next) ->
    routeMvc(req.params.controller, req.params.method, req, res, next)

  # If all else failed, show 404 page
  app.all "*", (req, res, next) ->
    console.warn "error 404: ", req.url
    res.statusCode = 404
    res.render "404", 404

# render the page based on controller name, method and id
routeMvc = (controllerName, methodName, req, res, next) ->
  controllerName = "index" if not controllerName?
  controller = null
  try
    controller = require "./controllers/" + controllerName
  catch e
    console.warn "controller not found: " + controllerName, e
    next()
    return
  data = null
  if typeof controller[methodName] is "function"
    actionMethod = controller[methodName].bind controller
    actionMethod req, res, next
  else
    console.warn "method not found: " + methodName
    next()

在我的index.coffee中就像:

...

app.use blade.middleware(process.cwd() + "/views")
# Initialize routes
routes = require "./routes"
routes(app)
app.use app.router

#### Finalization
# Register i18next AppHelper so we can use the translate function in template
i18n.registerAppHelper(app)
app.locals.pretty=true

# Export application object
module.exports = app

这不能正常工作,因为没有404路由正在服务!

routeMVC正在运行,因为我收到了console.log警告:

controller not found: calendar { [Error: Cannot find module './controllers/calendar'] code: 'MODULE_NOT_FOUND' }
error 404:  /calendar

我错过了什么?

0 个答案:

没有答案