Things to know as a BackEnd Developer
On call: when you work for a company then you have to support your application 24/7 hours.
Any time any issues come then you have to go and fix that.
staff engineering mean architect.
HLD = High Level Design
LLD = Low Level Design
1. Estimation: when we build an application it need estimation.
estimation like an App under app we need Compute/Storage/Network band with etc.
2. In NodeJs Application you can break whole back end into 3 three different layer.
These layer are:--
i. Routes
ii. Controller
iii. Model.
2.
server.js/index.js: this file is a starting point of every application of NodeJs.
Second create Model folder
3rd create Controller folder
4th create Route.
3. what is Database pool ?
4.
sequelize : this small s sequelize signifying the Database connection.
Sequelize : this capital S sequelize signifying the sequelize dependencies, class, etc
5.
In nodeJs project, there are three different layers.
i. Routes
ii. Controllers
iii. Models
server.js : This is a file where application start. This is Application starting file. It can be server.js or index.js
6.
How to solve below problem.
TypeError: Cannot read properties of undefined (reading 'name')
into server.js file add below line and import body-parser
server.js file
const bodyParser = require('body-parser');
// Registering body-parser middleware
app.use(bodyParser.json());
7.
There are two kinds of params.
i. path params.
it ends point something like this "/ecomm/api/v1/categories/123" 123----> is the path params
ii. Query params.
it ends point like this "/ecomm/api/v1/categories?name=vishwa" name=vishwa -----> is the query params.
What are the value come after ? (question mark ) that is called query params.
query param is optional.
You must know:
Controller is the whole brain system. Controller communicate with model.
model communicate with database.
route communicate with Controller.
Route <---------------> Controller <-------------------------> Model <-------------> Database
model is the first layer to create and follow MVC pattern.
first create model file.
second create controller file.
third create route file.
-----------------------------Let's program an ecommerce app--------------------------
First make a directory and open it with VS code.
Now open terminal from vs code. Shortcut key: Ctrl + Shift + back-trick key
Now on terminal type npm init and enter and finish the form and enter again.
Now install express, body-parser, mysql2,sequelize and dotenv packages.
Now check these package install properly or not. Go to package.json file and check the dependencies.
If not install any how install those packages using yourself by googling/stackoverflow no matter how many time it takes.
Now create a file server.js file.
Now create a server into server.js
index.js inside a module:
why we create index.js file inside model directory ?
sol: Index.js file inside a module it representative of fact that this single file will export the functionalities of all the others file. This is number one reason.
Number two is when you make of use index.js file this just put the module and index.js file is implicitly added to it. you don't need to specify more..
Let say a module have 10 differents file and i need the power of all 10 files. I would just go and call require module/model name. By default it will understand if a file name not given, it will aspect that they will something called as an index.js file which have all the information.
So that's why, we need index.js file.
in index.js file we need 2 thing.
i. we created our Database(DB) connection.
ii. we are exposing multiple things.
we are exposing sequelize (small sequelize)
we are exposing Sequelize ( capital sequelize)
and exposing it's category schema objects.
in the end we are exporting it by
modul.exports = DB
Error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
After sending the response, it actually execute the next line and goes to the next line actually it going to the controller but it did not go. So you have to put return into middleware file.
This is your middleware requestValidator.js file
/**
* This file will consists of middleware for validating the request body.
*
*/
/**
* Validate the request body for categories.
*/
const validateCategoryRequest = (req,res,next)=>{
/**
* Check for name
*/
if(!req.body.name){
res.status(400).send({
message : "Name of the category is not provided."
})
return; // this is the reason you got above error. u did not put return
}
/**
* Check for the description.
*/
if(!req.body.description){
res.status(400).send({
message: "Description of the category is not provided."
})
return; // this is the reason you got above error. u did not put return
}
/**
* Go to the controller.
*/
next();
}
/**
* Validator for the products request body.
*/
const validateProduct = (req,res,next)=>{
/**
* Check for name
*/
if(!req.body.name){
res.status(400).send({
message: "Name of the product is not provided."
})
return; // this is the reason you got above error. u did not put return
}
/**
* Check for the description.
*/
if(!req.body.description){
res.status(400).send({
message: "Description of the product is not provided."
})
return; // this is the reason you got above error. u did not put return
}
/**
* Check for the cost.
*/
if(!req.body.cost <= 0){
res.status(400).send({
message: "Cost doesn't seem to be in place"
})
return; // this is the reason you got above error. u did not put return
}
/**
* Go to the controller.
*/
next();
}
module.exports = {
validateCategoryRequest : validateCategoryRequest
}
error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
remove next() from the last. into your request.validator.js file.
next() two times called and thats why you get that error.
you have to learn from this error: inside one middle ware only returning one next.
Note: Next is a framework. Like expressJs.
Question: What is strongly typed Language ?
Strongly type language mean it a kind of language which we define a variable and also we say what type of that variable is.
For example JAVA,C, C++ etc.
Authentication: It mean you are the person, you are saying. You are proofing yourself.
That's typically called Authentication.
Authorization: It mean, What you can do.
UN-authenticated API : it mean no security in API.
Note: Multi-Factor Authentication (MFA):
Multi-Factor Authentication (MFA): 2 or more ways for authentications.
Like Banking payment system is a good example of MFA.
How MFA works in payment system:
i. first it require login to internet banking.
ii. Sometimes OTP also come into the phone. (OTP is common)
iii. Sometimes it need debit card/bank account number also required/ask.
So It's basically required/ask 2 or more authentication. And lastly it's called MFA.
Note: (MFA): Implement of Authentications:
It's required below details to log in:
i. User Registration.
ii. User sign in
Note: FOr Aruthorization:
After sing in an user , now what can do user. According to user roles it can be a normal client/admin/others. He can do something according to their roles.
Admin, customer/client, others. These are Roles and Re-strictly applied into the Roles.
Something they can do Something and Something they can't do.
Note:
How to design a Multi factor Authentication:
To make an MFA
You always follow these to make an MFA. Recommend.
i. first we go to create a Model.
ii. make a Controller .
under Controller make Sign Up and Sign In.
After Sign In make Token. (These all Signup/sign in, Token into the Controller.)
Token will be Generate with the help of GWT.
iii. Make Route:
Under Route make signUp and Sign In.
Note: How to Generate token ?
For Generating the Token there are so many options available in the market.
But the one of these is which mostly using now a days called JWT. (JSON Web Token).
RFC: (Request For Comments): When ever someone propose something new, for that they write a documents and name that document as RFC. This is called RFC.
Remember: No need to Remember. And it's not like a history class. All are available into the internet. Just Understand.
Any JWT always look like this below:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdW
IiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9
lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMe
F2QT4fwpMeJf36POk6yJV_adQssw5c
So whenever you make a JWT tokens. There are three parts always need to follow.
i. Header:
Header contains the information that which Algorithms used for creating this encoding.
For example HS256 is a standard algorithms. Which help to encrypt the data.
ii. Payload:
Payload is a main thing or data. Which you want to get the access. For example somebody log in so for you the payload will use username and password. Base on that you would have to encode this.
So, payload is the actual data. which you like to create this final token.
iii. Verify Signature:
Verify Signature mean it will take the header and it will encode the header. So after Header has been encode, this is a portion that you get.
Other Notes:
is Encryption/ Decryption and Hashing same.
No, Because
A--------------------> B "A encrypt B "
A <--------------------B " B decrypt A"
in Hashing A encrypt B, but B never Decrypt A
you can not come back in Hashing.
So, that's the encryption/decryption and hashing happening.
Boiler plate code : Boiler plate code mean when achieving small thing, we have to write large amount of code that called as Boiler plate.
It is not suggested and not scalable.
Testing
Testing: Testing is the process of verifying if the actual behavior is matching with expected behavior.
Types of Testing:
Black box testing: without knowing how it implemented, that called as a black box testing.
we just check the functionality, we don't know how the code return and how code is implemented.
e.g. UI testing, we don;t know it back end part how the code is implemented and more. we just testing it's UI functionality.
This is called as black box testing.
White box testing: White box testing is where Who try to test the code or internal implementation or the API testing this kind of testing called as white box testing.
It is also done by a dedicated QA engineer.
Testing is based on the functionality.
Functional Testing: Check if the functionality is as per the expectation.
e.g: suppose user want search functionality.
Non Functional Testing:
e.g: response/performance time/ Code maintainability/ Security/Testing/Scalable these all are called non functional testing.
Testing from the prospective of developer.
There are two types of testing for developer.
- Unit testing
It is used for unit peace of code. Testing each individual function.
it just support one functionality.
Pure Function only depends on the input.
- Integration testing:
This type of testing which involve multi entities, we called it integration testing. It have multiple files and multiple functionalities.
Integration testing is good for overall functionality.
Integration testing is not good for quality of single file.
TDD: Test Drive Development.-----> This is developer QA engineer part
BBD: Behaviour Drive Development.-----> This is QA engineer part
Test
install jest frame work -----> npm install jest --save-dev
jest is a testing frame work for javaScript.
In jest, it require 2 argument, one describe the test as a string and second is a call back function.
Below is a demo of jest test:
How to define a test:
Test also as a function
Name of the test.
call back function.
test("First test in JS", ()=>{
// Logic for testing
console.log("Hello Testing.");
})
This is gitgub basic
/**
* Steps for the questions asked in the class.
*
* Raise a PR to the github repo: https://github.com/vishwa07de/oooreto/
*
*
*
* Step 1: Fork it to your remote repo
* Step 2: Clone it locally
* git clone <url>
* Step 3: Create a branch - student
* git checkout -b student
* Step 4: Create a file inside the folder abc.txt I am amynur
* Step 5: git add .
* Step 6: git commit -m "Commiting the new file"
* Step 7: git push -u origin student
*
*
* Step 8: Come to the github url and raise the PR again /vishwa07de/oooreto/
* main branch
*/
Deployment
Deployment: Deployment mean you enabling your application to be accessible all the time.
- Start the server on a port. (This is the first part of deployment)
Server: Server is a never stopping process. It always up and running.
Consistency data: Consistency data mean All the data is same type.