server.js:
import itemRouter from './resources/item/item.router'
export const app = express()
app.use('/api/item', itemRouter)
item.router.js
import { Router } from 'express'
const router = Router()
const mockController = (req, res) => {
res.json({ message: 'ok' })
}
// /api/item
router
.route('/')
.get(mockController)
.post(mockController)
// /api/item/:id
router
.route('/:id')
.get(mockController)
.put(mockController)
.delete(mockController)
export default router
test:
import router from '../item.router'
describe('item router', () => {
test('has crud routes', () => {
const routes = [
{ path: '/', method: 'get' },
{ path: '/:id', method: 'get' },
{ path: '/:id', method: 'delete' },
{ path: '/:id', method: 'put' },
{ path: '/', method: 'post' }
]
routes.forEach(route => {
const match = router.stack.find(
s => s.route.path === route.path && s.route.methods[route.method]
)
expect(match).toBeTruthy()
})
})
})
标签:route,Express,item,mockController,path,Test,router,method From: https://www.cnblogs.com/Answer1215/p/16613950.html