const { authMiddleware } = require('./utils/authMiddleware')
var app = express();
app.use('/api', authMiddleware, require('./routes/upload-image'))
app.use('/api', require('./routes/user'))
app.use('/api', authMiddleware, require('./routes/task'))
app.use('/api', authMiddleware, require('./routes/credit'))
我有authMiddleware.js,它有一个简单的console.log
module.exports.authMiddleware = function(req, res, next) {
//res.body.user_id = )
console.log('get logged in user then set user id to body')
next()
}
后映像端点就像这个routes / upload-image.js
const express = require('express');
const router = express.Router();
router.post('/upload-image', function(req, res) {
res.json('ok')
});
router.get('/upload-image', function(req, res) { ... })
module.exports = router;
当我调用localhost:3000 / api / upload-image时,我在authMeiddleware中获得了两次控制台,为什么会触发两次?