koa2路由提示router.usre is not a function

胡说霸道的英雄
  • 19

koa2路由提示router.usre is not a function

我使用koa2-generator生成的,里面自动生成了index和users两个文件,列示的index就可以,但是我添加users就一直提示router.usre is not a function不知道为什么,麻烦各位大佬帮忙解释一下这个到底是什么原因,感谢大佬。

相关代码

配置文件

{
  "name": "koaapp",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "debug": "~2.6.8",
    "koa": "^2.3.0",
    "koa-bodyparser": "^4.2.0",
    "koa-convert": "^1.2.0",
    "koa-json": "^2.0.2",
    "koa-logger": "^3.0.0",
    "koa-onerror": "^3.1.0",
    "koa-router": "^7.2.1",
    "koa-static": "^3.0.0",
    "koa-views": "^6.0.2",
    "nunjucks": "~3.0.0"
  },
  "devDependencies": {
    "babel-eslint": "7.1.1",
    "eslint": "3.18.0"
  }
}

app.js代码

const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()

const views = require('koa-views')
const co = require('co')
const convert = require('koa-convert')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const debug = require('debug')('koa2:server')
const path = require('path')

const config = require('./config')
const routes = require('./routes')
// 就是这个,不行
const usre = require('./routes/users')

const port = process.env.PORT || config.port

// error handler
onerror(app)

// middlewares
app.use(bodyparser())
  .use(json())
  .use(logger())
  .use(require('koa-static')(__dirname + '/public'))
  .use(views(path.join(__dirname, '/views'), {
    options: {settings: {views: path.join(__dirname, 'views')}},
    map: {'njk': 'nunjucks'},
    extension: 'njk'
  }))
  .use(router.usre())
  .use(router.allowedMethods())
  

// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - $ms`)
})

router.get('/', async (ctx, next) => {
  // ctx.body = 'Hello World'
  ctx.state = {
    title: 'Koa2'
  }
  await ctx.render('index', ctx.state)
})

usre(router)
app.on('error', function(err, ctx) {
  console.log(err)
  logger.error('server error', err, ctx)
})

module.exports = app.listen(config.port, () => {
  console.log(`Listening on http://localhost:${config.port}`)
})

users.js

module.exports =  (router) => {
  router.get('/', async function (ctx, next) {
    ctx.body = 'this a users index!';
  })
}
回复
阅读 1.7k
2 个回答

你那样有点奇怪,试试这样子的:

app.js

const user= require('./routes/users')
router.use('/user',  user.routes())

user.js

const Router = require('koa-router')
const userRouter = new Router()

userRouter.get('/', async function (ctx, next) {
    ctx.body = 'this a users index!';
})

module.exports = userRouter

解决了

const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const cors = require('koa2-cors');
const router = new Router()

const views = require('koa-views')
const co = require('co')
const convert = require('koa-convert')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const debug = require('debug')('koa2:server')
const path = require('path')

const config = require('./config')
const routes = require('./routes/users')
const indexs = require('./routes/index')

const port = process.env.PORT || config.port

// error handler
onerror(app)
app.use(cors());

// middlewares
app.use(bodyparser())
  .use(json())
  .use(logger())
  .use(require('koa-static')(__dirname + '/public'))
  .use(views(path.join(__dirname, '/views'), {
    options: {settings: {views: path.join(__dirname, 'views')}},
    map: {'njk': 'nunjucks'},
    extension: 'njk'
  }))
  .use(router.routes())
  .use(router.allowedMethods())
//这里不要额外增加了,不管添加多少路由,这里都不要管了

// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - $ms`)
})


//这里对应上就好了
routes(router)
indexs(router)

app.on('error', function(err, ctx) {
  console.log(err)
  logger.error('server error', err, ctx)
})

module.exports = app.listen(config.port, () => {
  console.log(`Listening on http://localhost:${config.port}`)
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏