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

发布于 2022-09-12 02:27:09 字数 2509 浏览 31 评论 0

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!';
  })
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后彩虹 2022-09-19 02:27:09

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

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
素衣风尘叹 2022-09-19 02:27:09

解决了

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}`)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文