koa2路由提示router.usre is not a function
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你那样有点奇怪,试试这样子的:
app.js
user.js
解决了