我在下面設(shè)置了使用中間件調(diào)用API以在成功之前對其進(jìn)行授權(quán)。使用jquery ajax調(diào)用帶有令牌id的標(biāo)頭對象的API, $.ajax({ url : '/api/auth/fetchMycontent', type: 'POST', dataType: 'json', contentType: 'application/json; charset=utf-8', cache: false, context: this, headers: { "X-Access-Token": tokenId }, data: JSON.stringify({ accountId: accountId }), success: function(data){ //render data in HTML }});設(shè)置節(jié)點(diǎn)服務(wù)器如下,var express = require('express'); var app = express(); var bodyParser = require('body-parser');// Add your middlewares:var middleware = require("middlewares");app.use(bodyParser.json());app.all('/api/auth/*', middleware.apiValidate);app.use('/', require('./modelApi'));...在 modelApi/index 中設(shè)置 API.js, var express = require('express'), router = express.Router(), api = require('../api.js'), router.post('/api/auth/fetchMycontent', api.fetchMycontent); module.exports = router;中間件.js文件會像 module.exports = { apiValidate: function (req, res, next) { var token = req.body.x_access_token; if (token) { elastic.fetchToken(table, token).then(function (data) { if (data.hits.total > 0) { **//authorization success but next() fails here <------** next(); } else { res.json({ message: "Invalid User access!!", }); return; } }); } },};從api.js,fetchMycontent函數(shù)會像 fetchMycontent : function(req, res) { **console.log('not reaching after authorization success !!!')** elastic.fectMycontent(table, req.body).then(function (data) { res.set('Content-Type', 'application/json'); res.status(200); res.send(data); }) }當(dāng)我調(diào)用api'fetchMycontent'時(shí),它會按預(yù)期調(diào)用中間件并對其進(jìn)行授權(quán),并且不會調(diào)用fetchMycontent()!!!我在這里錯(cuò)過了什么?請告知
為什么節(jié)點(diǎn)js中間件在授權(quán)成功后使用Express不調(diào)用給定的API?
動漫人物
2022-08-18 15:39:26