Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Version 1
- ## Original Web App Controller Code for /api/postMessage:
- app.post('/api/postMessage', function(req, res){
- var ebWrapped1 = {
- payload: {
- token: req.get('token-auth'),
- method: 'post',
- path: '/api/postMessage',
- }
- }
- var ebWrapped2 = {
- identityId: null,
- payload: {
- channel: req.body.channel,
- text: req.body.text,
- }
- }
- eb.send('checkAuth', ebWrapped1, function(ebRes){
- if(ebRes.ok) {
- ebWrapped2.identityId = ebRes.identity._id;
- eb.send('/api/postMessage', ebWrapped2, function(ebRes2){
- if(ebRes2.ok){
- return res.status(200).json(ebRes2);
- } else {
- return res.status(ebRes2.statusCode).json(ebRes2);
- }
- })
- } else {
- return res.status(ebRes.statusCode).json(ebRes);
- }
- })
- })
- ===
- Notes on the above: 30 Lines, hard to read and follow, a little spaghetti with the 4 parts of method construction intertwined.
- ## Here is the Original Web App Controller Code again for /api/postMessage with the '4 parts of method construction' marked:
- app.post('/api/postMessage', function(req, res){
- # Collect Input
- var ebWrapped1 = {
- payload: {
- token: req.get('token-auth'),
- method: 'post',
- path: '/api/postMessage',
- }
- }
- # Collect Input
- var ebWrapped2 = {
- identityId: null,
- payload: {
- channel: req.body.channel,
- text: req.body.text,
- }
- }
- # Perform Work
- eb.send('checkAuth', ebWrapped1, function(ebRes){
- if(ebRes.ok) {
- # Deliver Output # Collect Input
- ebWrapped2.identityId = ebRes.identity._id;
- # Perform Work
- eb.send('/api/postMessage', ebWrapped2, function(ebRes2){
- if(ebRes2.ok){
- # Deliver Output
- return res.status(200).json(ebRes2);
- } else {
- # Handle Failure
- return res.status(ebRes2.statusCode).json(ebRes2);
- }
- })
- } else {
- # Handle Failure
- return res.status(ebRes.statusCode).json(ebRes);
- }
- })
- })
- ## Here is a refactored version with extra comments:
- Given both postMessage and checkAuth are located behind event bus:
- app.post('/api/postMessage', function(req, res){
- // Collect Input
- var payload = {
- token: req.get('token-auth'),
- channel: req.body.channel,
- text: req.body.text
- }
- // Perform Work
- backend.postMessage(payload, function(backend_response){
- // Deliver Output
- if(backend_response.ok){
- return res.status(200).json(backend_response);
- // Handle Failure
- } else {
- return res.status(backend_response.statusCode).json(backend_response);
- }
- })
- }
- Here is the same Original behavior version but refactored and without extra comments:
- app.post('/api/postMessage', function(req, res){
- var payload = { token: req.get('token-auth'), channel: req.body.channel, text: req.body.text}
- backend.postMessage(payload, function(backend_response){
- if(backend_response.ok){
- return res.status(200).json(backend_response);
- } else {
- return res.status(backend_response.statusCode).json(backend_response);
- }
- })
- }
- ===
- Notes:
- - Controller method complexity and LOC from 30 lines to 10 lines
- - No nested IF's
- - Clean using 'Tell Don't Ask' convention
- - Backend can take care of the auth itself
- - The backend library in node now is truly abstracted out so that:
- 1) the backend can be swapped out for a REST, WEBSOCKETS, GRPC2.... anything
- and the nodejs route/controller does not need to be touched.
- - Follows the 4 Parts of a method convention: Collect Input, Perform Work, Deliver Output, Handle Failures
- - Clear and well defined interface in the controller and backend allows us to swap out the backend easily since it is well, using a well defined small and simple interface.
- ## The backend js lib could be something similar to the below:
- Note: this library can be used from ANY application or node process. It is not intertwined with HTTP or PI cross boundary code:
- sopro.backend.postMessage = function(payload){
- payload.method = 'post';
- payload.path = '/api/postMessage';
- return eb.send('/api/postMessage', payload);
- }
- # Special Notes:
- The payload sent to the backend has everything it needs to take care of:
- 1) Checking the Auth and User ID lookup
- 2) Sanity Checks on Message/Channel destination
- 3) Actual persistence
- 4) Sending out a new event over the message bus containing the new message. Resulting in ALL node webapps listening on the event bus for this event to react to and render the new messages over websockets and in the GUI.
- 4) Returning a conventional response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement