Node MSA
Authentication
Node Cookie Session: use ^1.4.0 version
Udemy Course on cookie-session
::: warning Don't use cookie-session 2.0 :::
Error Handling
Needs to be handled consistently
Use an Abstract Error Class to simplify checking in the validation middleware. The benefit of using an abstract class over an interface is that the abstract class will persist through to javascript.
import { Request, Response, NextFunction } from 'express';
import { CustomError } from '../errors/custom-error';
export const errorHander = (err: Error, req: Request, res: Response, next: NextFunction) => {
if (err instanceof CustomError) {
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
}
res.status(400).send({
errors: [{ message: 'Something went wrong' }],
});
};
New Service
Step 1
Create a src folder
Step 2
Copy
- .dockerignore
- Dockerfile
- package.json
- tsconfig.json
- src/test
- src/index.ts
- src/app.ts
Step 3
Replace references to the old service with the new service. For example auth with tickets.
Delete references to the copied service's routes.
Step 4
npm install
Step 5
In the folder of the new service!!!
docker build -t stackmates/<new-service-name> .
docker push stackmates/<new-service-name>
Step 6
Setup k8s file for deployment
In the k8s folder create a copy of the depl for the original service. Example auth-depl.yaml to tickets-depl.yaml.
Then replace references to the old service with the new service name.
Step 7
Update skaffold.yaml to automatically sync changes
Copy the existing reference services config then update with the new service name.
- image: stackmates/<new-service-name>
context: <new-service-name>
docker:
dockerfile: Dockerfile
sync:
manual:
- src: 'src/**/*.ts'
dest: .
Check src is correct for code structure of the service.
Make sure indentation is correct
Step 8
Write k8s for DB deployment and service layer.
apiVersion: apps/v1
kind: Deployment
metadata:
name: <new-service-name>-mongo-depl
spec:
replicas: 1
selector:
matchLabels:
app: <new-service-name>-mongo
template:
metadata:
labels:
app: <new-service-name>-mongo
spec:
containers:
- name: <new-service-name>-mongo
image: mongo
---
apiVersion: v1
kind: Service
metadata:
name: <new-service-name>-mongo-srv
spec:
selector:
app: <new-service-name>-mongo
ports:
- name: db
protocol: TCP
port: 27017
targetPort: 27017
Step 9
Check skaffold build is working correctly.
skaffold dev
Any issues repeat 5.
Step 10
Setup the services routes.
Some common test cases.
xit('has a route handler at </api/--> to handle post requests', async () => {});
xit('can only be accessed if the user is authenticated', async () => {});
xit('returns an error if an invalid <fieldOne> is provided', async () => {});
xit('returns an error if an invalid <fieldTwo> is provided', async () => {});
xit('creates a <new-service-item> with valid inputs', async () => {});
Step 11
Add new routes then setup the ingress-srv.yaml file to route the traffic.
- Service layer
Data Management
Eventual consistency, how to manage data versioning