每次我尝试在添加用户路由后运行该应用程序时,都会出现类似这样的错误
package.json
这是我的文件结构
app.js
C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (C:\Users\adity\Desktop\thinkster\medium-api\node_modules\express\lib\application.js:210:11)
at Object.<anonymous> (C:\Users\adity\Desktop\thinkster\medium-api\app.js:15:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[nodemon] app crashed - waiting for file changes before starting...
routes / index.js
const app = express();
app.use('./routes');
routes / api / index.js
const express = require('express');
const router = express.Router();
router.use('/api', require('./api'));
module.exports = router;
routes / api / users.js
const express = require('express');
const router = express.Router();
router.use('/', require('./users'));
module.exports = router;
我找到了一个名为realworld.io的网站,并且正在遵循一种制作nodejs API的方法,在这里我每次都会陷入困境。而且我找不到问题所在。
答案 0 :(得分:0)
您的app.use('./routes');
中的代码app.js
不正确(只有一个字符串参数,违反了app.use()
syntax)。
要正确定义“路线”,代码应类似于:
// app.js
const app = express();
const routes = require('./routes');
app.use('/', routes);