0

我有这个路线。咖啡

#### 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

我错过了什么?

4

0 回答 0