Express.io express.Router()返回undefined

时间:2016-01-21 18:12:22

标签: node.js express coffeescript express.io

我正在为我的网络应用程序从express切换到express.io,但是当我运行应用程序时它会抛出一个错误,似乎与express.io的express.Route()返回undefined有关。我有办法解决这个问题吗?

/home/zunon/Development/Projects/xKout/router.js:35
router.get('/randomcard', function(req, res) {
      ^

TypeError: Cannot read property 'get' of undefined
    at Object.<anonymous> (/home/zunon/Development/Projects/xKout/router.js:35:7)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/home/zunon/Development/Projects/xKout/xkout.js:6:10)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

这是我的主要xkout.coffee文件,它被编译成xkout.js

express = require 'express.io'
router = require './router'
app = express()
app.http().io()

app.set 'view engine', 'jade'
app.set 'views', './views'

app.use '/', router
app.use '/public', express.static './public'

app.listen 3000, () ->
    console.log 'Listening on 3000'
    return

这是我的router.coffee,这是我的路由中间件

express = require 'express.io'
router = express.Router()

createCardName = ->
    position = Math.floor Math.random() * cards.length
    cards.splice position, 1

resetCards = ->
    for i in [1..13] by 1
        cards[i - 1] = "club#{i}"
        cards[i + 12] = "dmnd#{i}"
        cards[i + 25] = "hart#{i}"
        cards[i + 38] = "spad#{i}"
        if i < 3
            cards[i + 51] = "joke#{i}"

cards = []

resetCards()

router.get '/randomcard', (req, res) ->
        cardNames = {}
        for i in [1..6] by 1
            cardNames[i] = []
            for j in [1..9] by 1
                cardNames[i][j] = createCardName()
        console.log cardNames
        resetCards()
        res.render 'randomcard', {cardNames}
        return

module.exports = router

我发现其他人之前已经问过这个问题而没有得到答案: https://stackoverflow.com/questions/31222845/how-to-use-express-router-with-express-io

0 个答案:

没有答案