正确设置快速路线

时间:2014-03-28 10:23:54

标签: 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.

fs = require 'fs'

module.exports = (app) ->

  #   - _/_ -> controllers/index/index method
  app.all "/", (req, res, next) ->
    routeMvc("index", "index", req, res, next)
  fs.readdirSync(process.cwd() + "/app/controllers").forEach (file) ->
    controller = file.split(".")[0]
    app.all "/#{controller}", (req, res, next) ->
      routeMvc("#{controller}", "#{controller}", 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)
  # Robots.txt
  app.all '/robots.txt', (req, res) ->
    req.flash()
    res.set 'Content-Type', 'text/plain'
    if app.settings.env == 'production'
      res.send 'User-agent: *\nDisallow: /signin\nDisallow: /signup\n
          Disallow: /signout\nSitemap: /sitemap.xml'
    else
      res.send 'User-agent: *\nDisallow: /'
  # If all else failed, show 404 page
  app.all "/*", (req, res) ->
    console.warn "error 404: ", req.url
    req.flash('info', '404!')
    res.render '404',
      status: 404
      user: req.user

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

在我的控制台上获取以下内容时,从localesjs中排除controller not found: locales { [Error: Cannot find module '../controllers/locales'] code: 'MODULE_NOT_FOUND' }和{{1}}的正确方法是什么:

{{1}}

找不到控制器:locales {[错误:找不到模块' ../ controllers / js']代码:' MODULE_NOT_FOUND' }

因为我有一个允许用户在不同语言之间切换的JavaScript代码。

https://github.com/nkhine/node-blade-boiler-template/blob/master/public/js/locale.js#L169

任何建议都非常感激。

1 个答案:

答案 0 :(得分:0)

尝试添加此内容:

app.all /\controllers?\/(locale|js)/, (req, res) ->
  someFunction req, res

之前:

app.all "/:controller/:method", (req, res, next) ->
  ...

someFunction处理localejs案件的位置。