带有Express Mounted Route的Typescript类



尝试转换使用Express.js的Node.js项目。最终目标与我已经在apps .ts文件中得到的类似。在普通Javascript中,解决方案是相同的,但它不是一个类,而是一个模块。导出变量函数

export class UserRouter {
constructor() {}
// Open to all users
register() {
router.post('/account-register', AuthController.prototype.register);
}
login() {
router.post('/account-login', AuthController.prototype.login);
}
logout() {
router.get('/account-logout', AuthController.prototype.logout);
}
forgotPassword() {
router.post(
'/account-password-forgot',
AuthController.prototype.forgotPassword
);
}
resetPassword() {
router.patch(
'/account-password-reset/:token',
AuthController.prototype.resetPassword
);
}
// Require Login for all subsequent routes
protectedRoutes() {
router.use(AuthController.prototype.protectedRoutes);
}
updateUserProfile() {
this.protectedRoutes;
router.patch(
'/account-update-profile',
UserController.prototype.uploadUserPhoto,
UserController.prototype.updateMyProfile
);
}
updateUserSettings() {
this.protectedRoutes;
router.patch(
'/account-update-settings',
UserController.prototype.updateUserSettings
);
}
deactivateUserAccount() {
this.protectedRoutes;
router.delete(
'/account-deactivate',
UserController.prototype.deactivateUser
);
}
// Only the delcared user roles can access the subsequent routes
limitedAccessByUserRole() {
router.use(
AuthController.prototype.restrictToRoles(
'employee-admin',
'employee-super-admin'
)
);
}
getAllUsers() {
this.protectedRoutes;
this.limitedAccessByUserRole;
router.route('/').get(UserController.prototype.getAllUsers);
}
createUser() {
this.protectedRoutes;
this.limitedAccessByUserRole;
router.route('/').post(UserController.prototype.createUser);
}
getUser() {
this.protectedRoutes;
this.limitedAccessByUserRole;
router.route('/:id').get(UserController.prototype.getUser);
}
updateUser() {
this.protectedRoutes;
this.limitedAccessByUserRole;
router.route('/:id').patch(UserController.prototype.updateUser);
}
deleteUser() {
this.protectedRoutes;
this.limitedAccessByUserRole;
router.route('/:id').delete(UserController.prototype.deleteUser);
}
}

类通过变量UserRouter导入到App.ts文件中。

// Mounted API Routes
const apiVersion = '1.0';
const apiRoutes = `/api/v${apiVersion}`;
app.use(`${apiRoutes}/users`, UserRouter);

寻找解决方案。非常感谢任何帮助。完整的项目代码(减去ENV文件)在Github上。

你可以尝试为所有的路由创建一个class属性,就像这样:

user.network.ts

export default class UserRoutes {
public router: Router;
// remplace my controller for your controller
private controller: UserController = new UserController();
constructor() {
this.router = express.Router();
this.registerRoutes();
}
// remplace my example routes and controller methods for your own 
protected registerRoutes(): void {
this.router.get('/:username',this.controller.getUserByUsername);
this.router.get('/', this.controller.paginationByUsername);
this.router.post('/', this.controller.createUser);
this.router.post('/login', this.controller.login);
this.router.put('/', this.controller.editUser);
}
}

routes.js

import express from 'express';
import UserRouter from './userRoutes.ts'
// if you want to add another router like news or something else
// you could add one 'server.use(...)' below the 'server.use('/user',...)
const routes = (server:express.Application): void => {
server.use('/user', new UserRoute().router);
};
export default routes

在server.js中,你可以这样写:

class Server {
public app: express.Application;
constructor() {
this.app = express();
this.config();
}
public config(): void {
this.app.set('port', 3000);
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
routes(this.app)
}
public start(): void {
this.app.listen(this.app.get('port'), () => {
console.log('Server listening in port 3000');
});
}
}
const server = new Server();
server.start();

最新更新