我在下面设置了使用中间件调用API以在成功之前对其进行授权。使用jquery ajax调用带有令牌id的标头对象的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 }});设置节点服务器如下,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 中设置 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函数会像 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); }) }当我调用api'fetchMycontent'时,它会按预期调用中间件并对其进行授权,并且不会调用fetchMycontent()!!!我在这里错过了什么?请告知
添加回答
举报
0/150
提交
取消