How we can use the concept of dependency injection in route file and added in server file?
I tried to use dependency injection in my node project but I don't think that I am on the right path. I am trying to inject my UserService into my AppRouter. My registration method was previously static but I changed it to a public method because a static member does not allow me to inject the dependency.
First of all this.userService.UserRegistrationService is showing me error undefined. Please suggest the right way to do this as I am new to express and node.
Route File
import express from 'express';
import bodyParser = require('body-parser');
import { API } from ".././utilities/api";
import UserService from '../api/services/UserService';
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
import validationMw from '../api/middlewares/request_validation';
import UserDao from '../api/requests/userDto';
let cors = require('cors');
//let bodyParser = require('body-parser');
class AppRouter {
private userService: UserService;
public router: express.Router;
public userObj: any;
constructor(userService: UserService) {
this.router = express();
this.router.use(cors());
this.AddMiddlewares();
this.AddRoutes();
this.userService = userService;
}
// Add all middlewares here that will get request before routes.
AddMiddlewares(): void { // Used to parse req.body into json object
this.router.use(bodyParser.json());
this.router.use(bodyParser.urlencoded({ extended: false }));
//this.router.use(LogsService.LogRequest);
}
AddRoutes(): void {
console.log(API.RegistrationEmployee);
this.router.post(API.RegistrationEmployee, this.userService.UserRegistrationService);
}
}
export default new AppRouter(new UserService).router;
User Services Class
import { Request, Response } from "express-serve-static-core";
import { isNullOrUndefined, isNull } from "util";
import { logger } from "../../utilities/logger";
import UserController from "../controllers/userController";
import { ProjectConstants, ErrorCodes } from "../../utilities/config";
import {
ReasonPhrases,
StatusCodes,
getReasonPhrase,
getStatusCode,
} from "http-status-codes";
import ResponseService from "../services/ResponseService";
export default class UserService {
private userController: UserController;
constructor(userController?: UserController) {
this.userController = userController;
}
public async UserRegistrationService(req: Request, res: Response) {
console.log(434);
// UtilityMethods.ConsoleLog("UserRegisterService - BODY", req.body);
logger.info("UserRegistration- function call " + JSON.stringify(req.body));
await this.userController.registration(req.body)
.then((data: any) => {
// UtilityMethods.ConsoleJSON("03 GetCategoryService", data);
ResponseService.SendSucessResponse(req, res, data, ErrorCodes.OK);
})
.catch(function (err: any) {
ResponseService.SendErrorResponse(req, res, err);
});
}
}