我正在使用 express.js ,并尝试在单独的文件中分隔我的路线。
这是我的 ./ app.coffee :
的摘录viewsRouter = express.Router()
routesCallingViews = require('./app/routes/callingViews')
app.use '/', routesCallingViews.showApp viewsRouter, user, data
app.use '/', routesCallingViews.showSignUpForm viewsRouter
app.use '/', routesCallingViews.showLogInForm viewsRouter
./ app / routes / callingViews 如下所示:
module.exports =
showApp: (router, user, data) ->
router.get '/', user.isSignedIn, (req, res) ->
...
showSignUpForm: (router) ->
router.get '/signup', (req, res) ->
...
showLogInForm: (router) ->
router.get '/login', (req, res) ->
...
我收到此错误:
Error: Route.get() requires callback functions but got a [object Undefined]
有什么建议吗?
答案 0 :(得分:0)
执行您尝试的操作的常用方法是将所有相关路由提取到单独的模块,您将在其中创建和导出路由器实例,例如:
svg
然后从任何地方(通常来自// foo.js
'use strict'
const express = require('express')
const router = express.Router()
router.get('/', (req, res, next) => { ... })
router.post('/bar', (req, res, next) => { ... })
...
module.exports = router
)你可以挂钩你拥有的所有路由器,例如:
server.js
所以你最终会得到:
const fooRoutes = require('./foo')
app.use('/foo', fooRoutes)