我想将特定的中间件功能应用于两个post
请求,而不是get
请求。我怎么能这样做?
var router = express.Router();
var app = express();
router.post('/jobs',(req,resp)=>{
var messageString = JSON.stringify(req.body);
Job.accept(messageString,(statusCode,respObject)=>{
resp.status(statusCode).json(respObject);
});
});
router.get('/jobs',(req,resp)=>{
Job.status((statusCode,respObject)=>{
resp.status(statusCode).json(respObject);
});
});
router.post('/try',(req,resp)=>{
var messageString = JSON.stringify(req.body);
Job.ok(messageString,(statusCode,respObject)=>{
resp.status(statusCode).json(respObject);
});
});
我正在阅读app.use,但无法真正了解其用法。
答案 0 :(得分:0)
将您想要的中间件添加到该功能中。以下是我记录用户IP的示例
const myLogger = (req, res, next) => {
console.log('got a request from', req.connection.remoteAddress);
next();
}
app.post('/jobs', myLogger, (req, res) => {
//your code here...
}
答案 1 :(得分:0)
除了上面提到的答案外,请写下app.use(myLogger)