50 likes | 52 Views
The main aim of service-oriented architecture is to allow users to mix large chunks of functionalities into one in order to build an application and services by combining services. This architectural design of node backend application is considered a best practice in industrial labels. Read more on Service Oriented Architecture in NodeJS
E N D
From the Resource Library of Andolasoft.Inc | Web and Mobile App Development 1 Company
Service Oriented Architecture in NodeJS What is Service oriented Architecture in NodeJS? Serviceoriented architecture is a way to Building a backend applications by consisting multiple independent services. A service can be any business logic that completes an action and provides a specified result to the end point user. Each service in SOA is a complete business function in itself. For an Example Let’s discuss about a grocery store. The owner add the product details in the inventory. Then owner search to find data about a unique product. And the backend application database query should be inside the service section by leveraging data access layer. Then it securely fetch data and give the response to the owner. Layers in Service oriented Architecture Router layer Service layer Data access layer How SOA Works ●Here first when API call, in node app first it face router section where all the authentication and validation occur through the middleware function. Then control moves towards controller section which work is collect the incoming data related to APIrequest. Here all the business logic to access data are combined inside a service. So the controller invokes that service and service leverage data access layer to connect with database and calculation. Then services return the data to the controller. After that controller give the response to the end point user. ●The most important benefit in SOA is as all the services are independent and small chunks so whenever we want to change our programing language it should be very fast and easy in development process. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development 2 Company
Flow chart diagram: SOA Advantages ●Reliable– Even though service-oriented architecture has small and independent services, it becomes easier to debug code as it checks small chunks instead of checking a large codebase ●Independent of location– Services can be accessed through URL therefore change in location doesn’t affect customer experience ●Scalable– Service-oriented architecture can run on multiple platforms and can operate various servers within the given environment ●Reusability– The SOA uses its services in multiple applications independently without disturbing other services ●Easy maintenance– SOA is an independent unit therefore updating becomes easy as it is a single entity ●Agile– Due to reusable components and services, it helps developers to integrate and build applications quickly according to requirements and consequently increases the agility of service-oriented architecture. Now question is why we shouldn’t did this directly inside the controller? From the Resource Library of Andolasoft.Inc | Web and Mobile App Development 3 Company
As router built with express so it combined with req and res object.So when we put our business logic inside controller then at the testing we have to write mock for test with response mock. The problem is when we try to only test our business logic we can’t do that but when we combined that inside service we can test that service without req, res object. Code Example: In this example we create a service that to resister a user in data base for sign up API. In user.js controller receive all the request data and call the user service // controllers/user.js constUserService=require( "../services/UserService" ); constUserServicesec=newUserService(); asyncfunctionmakeUser ( req, res ) { try { letbodyData=req.body; constmakeUser=awaitUserServicesec.create( bodyData ); returnres.send( makeUser ); } catch( err ) { res. status(500 ). send( err ); } } module.exports={ makeUser }; Then in the user service folder import the data access layer and database model then service used that data access layer and execute query in database // services/UserService.js // Data Access Layer constMongooseService=require( "./MongooseService" ); From the Resource Library of Andolasoft.Inc | Web and Mobile App Development 4 Company
constuserModel=require( "../models/user" ); classUserService { constructor () { this.MongooseService=newMongooseService(userModel ); } asynccreate ( createUser ) { try { constresult=awaitthis.MongooseService.create( createUser ); return { success:true, body:result }; } catch( err ) { return { success:false, error: err }; } } } module.exports=UserService; Conclusion: The main aim of service-oriented architecture is to allow users to mix large chunks of functionalities into one in order to build an application and services by combining services. This architectural design of node backend application is considered as a best practice in industrial label. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development 5 Company