路由地址报错404

来源:3-2 Koa异步async

慕的地3227104

2018-11-25

const router = require(‘koa-router’)()

router.get(’/’, async (ctx, next) => {
global.console.log(‘index’)
await ctx.render(‘index’, {
title: ‘Hello Koa 2!’
})
})

router.get(’/string’, async (ctx, next) => {
ctx.body = ‘koa2 string’
})

router.get(’/json’, async (ctx, next) => {
ctx.body = {
title: ‘koa2 json’
}
})

router.get(’/testAsync’, async (ctx) => {
const a = await new Promise((resolve) => {
setTimeout(() => {
resolve(‘a’)
}, 1000)
})
ctx.body = {
a
}
})

module.exports = router

老师,为什么我写的那个/testAsync,接口会报404呢,我以为我写错了,于是我把上面的实例代码复制了一次,改了个接口名字,还是报404,这是什么原因啊?

写回答

2回答

慕粉3226958

2019-06-11

怎么解决的 我遇到了相同的问题

0
1
慕粉3226958
重新npm start就好了,不能和vue一样热刷新吗?
2019-06-11
共1条回复

快乐动起来呀

2018-11-25

其他接口有生效吗,应该是这个路由文件没有在app.js引入并加到到路由表,对照源码看下哈

0
2
GhostCatCG
回复
慕的地3227104
怎么解决的呀兄弟 回一下
2019-07-09
共2条回复

Vue全家桶+SSR+Koa2全栈开发美团网

整合大前端8项技术,全面晋级全栈工程师,毕设/面试作品

2445 学习 · 1638 问题

查看课程