var category = require('../controllers/category/category.controller')
var _verifyToken = require('../../config/verify_jwt_token')
var prefix = '/category'
module.exports = function (app) {
app.route(prefix + '/categories').all(_verifyToken.verifyToken).get(category.getAllCategories)
}
x-access-token标题可在 getAllcategories 中使用。 get(category.getAllCategories) 控制器,但是当我将请求传递给 .all(_verifyToken.verifyToken)函数时。我丢了令牌。
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
)
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, content-type, x-access-token, Authorization'
)
res.setHeader('Access-Control-Allow-Credentials', false)
next()
})
只有在Angular 6中才发生这种情况。对于postman和angularJS 1,它可以正常工作
角度令牌拦截器
constructor( private _authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// let authService:
console.log('Here is the token interceptor');
// const token = this._authService.getToken();
// console.log(token);
const tokenizedReq = req.clone({
setHeaders: {
'Content-Type': 'application/json"',
'x-access-token': `${this._authService.getToken()}`
}
});
return next.handle(tokenizedReq);
}
角度请求
_getAllCategoriesUrl = environment.APIEndPoint + '/category/categories';
getCategories() {
return this.http.get<any>(this._getAllCategoriesUrl);
}