我在一个类中有一个ES6函数,
initRestServices() {
new UnprotectedRestService(this.restServer.getRouter("/users"));
this.restServer.setMiddleware();
new UserRestService(this.restServer.getRouter("/users"));
}
因此,我在公共休息服务之后设置身份验证中间件。 当我使用nodemon运行此文件时,它将按预期运行。 此函数的转译js如下,
{
key: "initRestServices",
value: function initRestServices() {
new _unprotectedRest2.default(this.restServer.getRouter("/users"));
this.restServer.setMiddleware();
new _userRest.UserRestService(this.restServer.getRouter("/users"));
}
}
我已经使用babel src -d dist -s
创建了该转载
现在,按照我的期望,应该在公共路由之后设置中间件。但是当我执行此操作时,我从公共路由后添加的中间件中收到身份验证错误。
我的RestServer类如下,
import Config from "./../config/config";
import { authRequest} from './auth.service';
import express from "express";
import bodyParser from "body-parser";
export default class RestServer {
app;
server;
constructor() {
this.app = express();
this.app.use(bodyParser.json());
}
getApp() {
return this.app;
}
setMiddleware(){
this.app.use((req,res,next) => authRequest(req,res,next));
}
getServer() {
return this.server;
}
startServer() {
this.server = this.app.listen(Config.property.portNo, () => {
console.log("Server has started on port no " + Config.property.portNo);
});
}
stopServer() {
this.server.close(() => {
console.log("Server is closed.");
});
}
getRouter(routerPath) {
var router = express.Router();
this.app.use(routerPath, router);
return router;
}
}
还有我的休息服务
export default class UnprotectedRestService {
app;
constructor(app) {
this.app = app;
this.initServices();
}
initServices() {
this.app.post("/", (req, res) => this.signup(req, res));
this.app.post("/login", (req, res) => this.login(req, res));
}
// detailed implementations
}
我正在将它们初始化为启动类,
initRestServices() {
new UnprotectedRestService(this.restServer.getRouter("/users"));
this.restServer.setMiddleware();
new UserRestService(this.restServer.getRouter("/users"));
}