1. const router = require("koa-router")();
    2. const MovieModel = require("../../models/movie");
    3. router.get("/api/search",async ctx=>{
    4. var {keyworld} = ctx.query; ##搜索关键字
    5. var reg = new RegExp(keyworld);
    6. var tables = ["top250","inTheaters","comingSoon"]; ##1.将三张表定义成一个数组
    7. var searchMovies = []
    8. for(let item of tables){ ##对数组进行for-of遍历
    9. var res = await MovieModel(item).find({title:reg}); ##传值
    10. searchMovies.push(...res); ##将搜索到三张表中的内容合成一个数组
    11. }
    12. ctx.body = {
    13. code:200,
    14. res:searchMovies,
    15. total:searchMovies.length,
    16. msg:"电影搜索"
    17. }
    18. })
    19. module.exports = router;