报错:route.routes is not a function

来源:5-4 更合理的目录结构

杭州程序员张张

2019-10-02

为什么我这样写和老师的一样,报错

TypeError: route.routes is not a function
    at fs.readdirSync.forEach.file (/Users/zhangbing/Downloads/code/zhuhu-api/app/routes/index.js:7:23)

代码如下:

const fs = require('fs')

module.exports = (app) => {
    fs.readdirSync(__dirname).forEach(file => {
        if (file === 'index.js') { return };
        const route = require(`./${file}`);
        app.use(route.routes()).use(route.allowedMethods());
    })
}

目录结构
图片描述

写回答

1回答

lewis

2019-10-02

把示例代码克隆下来可视化diff一下你和示例代码的区别。

0
2
杭州程序员张张
我知道什么原因了,我user.js里没东西。。。
2019-10-02
共2条回复

Node.js仿知乎服务端-深入理解RESTful API

Koa2+MongoDB+Postman+JWT+阿里云

952 学习 · 331 问题

查看课程