Intro to Backend Web Development – Node.js & Express Tutorial for Beginners

freeCodeCamp.org · Beginner ·🔧 Backend Engineering ·7mo ago

Key Takeaways

This video tutorial covers the basics of backend web development using Node.js, Express.js, and MongoDB, including server setup, database connection, and API creation for user authentication and CRUD operations.

Full Transcript

Welcome to your introductory guide on building a back end for your website. In this video, you'll learn how to construct a basic back end from the initial setup to handling the first user request. The back end is the important brain behind any website, responsible for managing user data, authentication, and communication with databases. The course uses popular technologies like the Node.js runtime environment, the Express.js framework, and the MongoDB NoSQL database. You'll learn the core components of back end development, including programming languages, databases, runtime environments, and frameworks. You'll also see a practical demonstration of how to connect your back end to a database and structure your code using a model, route, and controller pattern. Get ready to start coding your own web server and create your first authentication and CRUD APIs. And this course was created by a talented member of Hack Club. Hack Club is a worldwide, non-profit network of coding clubs where students learn to code by building real things with code together. Being part of Hack Club means joining a community that believes in the power of youth to build the future and provides the resources, mentorship, and opportunities to make it happen. You can learn more about Hack Club at hackclub.com. Have you ever wanted to build a back end for your website? In this video, we will learn how to build a basic back end for a website from its first setup to running its first request. Hi, I am Shivani, an 18-year-old developer from India. Let's start. Back end is the brain behind a website. It handles user data, authentication, and talks to databases. It makes sure the right response goes to the right request. So, basically, every button you click on a website, back end makes it do something. Core components of back end development. Okay, so the first one is a programming language. You will use a programming language to write your logic in, to write your back end server in. We can use JavaScript, Python, PHP, Go, C++, and a lot more. Then, we have a database. Okay. So, a database is like a storage box in which you keep all your data of the website. We have two kind of databases. One is SQL, another one is NoSQL. SQL databases are the more structured ones, in which all your data gets stored in tables, in rows and columns. It's like a spreadsheet. For example, PostgreSQL, SQLite. But, on the other hand, we have NoSQL databases. NoSQL databases are the more flexible ones, where you put your data in documents, in key-value pairs, and in graphs. For example, MongoDB. MongoDB is what we will use in this video to get a look in back end. Then, we have a runtime environment. A runtime environment is the underlying engine which runs your JavaScript code outside of the browser, on your local machine. So, JavaScript is just text. It won't magically run on your local machine. So, you need something to execute it, to interpret it. And for that, we need a runtime environment. We have a lot of runtime environments for JavaScript. We have Node.js, Deno, Bun, and a lot more. We will use Node.js in this video as we are writing our back end in JavaScript. But, if we write our back end in Python, we will use CPython interpreter. Then, we have a framework. So, what is a framework? A framework is something that helps you build your servers faster, and it it is basically just like a toolkit for you. So, you don't have to write all the back end in from scratch. And it hides all the complex, low-level stuff, which you don't need to worry about. For JavaScript, we can use express.js. Python has Django, Flask. Java has Spring Boot. And PHP has Laravel. We have a lot more, but these are the basic ones. Then, we can move on to this analogy. So, this is kind of a flowchart which I have made to understand this flow better. First of all, you will be using a programming language like JavaScript, in which your code and instructions will be. Then, you will use a engine that will run your code, which is Node.js. Then, we will use a framework, which will be express.js. It is basically a pre-built toolkit for us. So, even if you don't use a framework, we can make a back end, but I think using a framework is a lot more better. Then, you will use a database to store your data. So, every kind of data on the website which needs to be stored or retrieved at some point of time, you need to store it on a database. And all of this stuff makes a back end server. So, to handle your APIs, like requests and responses, you need to make all this together to make a back end server. Next, we have how a front end connects to a back end. So, on the left side, we have a browser, or maybe a user who is making a request. Let's say it's you on a website who is making a request. Your request goes to an API and take API like a waiter here. So, API is something which will take your request to the back end. Then, back end will go to the database, see all the stuff. Uh it will see if you need something from the database, if you want to retrieve something, or if you want to save something, and all this stuff will happen in the back end. And then, back end will send you back a response through the APIs. So, APIs have a very big role here. They are always in the between of you and the back end. So, APIs do a lot of work here. And here is a small box which I have created. So, when a client, that is you, the user, who makes a request to the server, server is the back end, then that is called a request. But, when the server, the back end, gives something to the client, that is a response. Okay. Let's start writing our back end. There are two must-have stuff which you should be having. First is VS Code because we will be coding in VS Code, and the another thing is Node.js installed in it. Now, let's take a look on how to install Node.js on your local machine. For that, we can go to the nodejs.org website. This is the official website of Node.js. From here, we can download Node.js for our computer. This is the download button. You can click on it, and then you get redirected to this page. Here, you can select the version of Node.js you want to install on your computer. For that, we have the current version, which is the most latest one. Then, we have an LTS one. But, the current version is not the most stable one you can find. So, the LTS one is the most stable one to download. So, everybody recommends that. Next is the operating system you are on. So, select the operating system you're on. I am on Mac, so I will select Mac. These two things remain the same. And then, by this button, you have to install the package. So, when you have successfully installed the package, you can just go on to your VS Code. And in the terminal, you can write node {hyphen} v. When you enter, you get to see the version of Node.js your computer now has. If you see a version like this, that means you have successfully installed it. Or, if you don't see it, then it's not installed. Now, let's hop on to our sheet, and let's see the next thing, which is the folder structure. For the project we will be making in this video, the back end and the front end are going to be split up into two different folders, and for the ease to maintain them. So, back end and front end are going to be different folders. Nextly, let's have a deeper look into the back end folder. So, these are the files which we will be making in this video. The front end will be empty because we are not working on the front end. We are learning intro to back end in this video. Now, let's start the project. So, we will go to the desktop, make a new folder named intro to back end. Okay. And then, we will drag this into the VS Code app. Now that I have this folder opened in the VS Code, I can start making my folders. So, the first folder which we will make is the back end folder, which we are going to work on. Nextly, I will make read me .md file because we want to initialize this repository. So, we have to write something. Now, let's initialize this repository by this button. And let's write a commit message which can be initial commit. And then we can commit this. I will make it a public repository. So, it has been pushed to GitHub and now we can start making other files. The first file which we need to make is the package.json file. So, what does this file do? This file is where all the metadata about your project is kept. Like all the libraries, all the dependencies, basically all these fancy things which you use for your project are kept here. All the data about them. So, to make this file, you have to go on to your terminal and then you have to write npm init. When you write this and you click enter, you get a set of questions to answer. So, the first question is the package name. It has written it intro to backend. I'm okay with it. I will click enter. Then the version. The version is one. And then a description. So, a description can be a a tutorial or intro to backend. Then the entry point is going to be the index.js file. We will be talking about how does this entry point is decided and how does it work in the later of this video. Test command is something the test scripts we will write ourselves. So, let's keep it like this for now. The Git repository which we just initialized comes here. And then keywords. If you want to write any keywords, you can. We will leave it empty for now. Then the author's name. Uh my name is Shivani, so I will write that. Then the license is ISC. Let's keep it. And then it will ask you, is this okay? And yes, we are okay with it. So, I will click enter. Now you can see we have a file here which is named as package.json. You can see it has a name, uh version, description, all the things which we wrote and clicked enter to. These are the scripts which we will be writing and enhancing over the video. The repository, author name, license. Yes, this is how these files look like. When you will install any dependency or any library, their names will come here. And we will see them further in the video. Once we have set up our backend repo, the first thing which we will do is to connect to a database. Our database is going to be MongoDB Atlas and to create that, we can go to the MongoDB Atlas website. So, this is the website. You go on it. You can create a account. And I am signing here because I have an account already. Okay, so this will look like this if when you have made an account here. When you go on this, you can see we get to see the projects we have made. I will click on this button and make a new project. Now, we have to write the name of our project. The name of project can be intro to backend. This is optional, so we will leave it empty. Then we click on the next button. Then you can add members and set permission. So, we don't need to do them. We will just create the project by clicking this button. Okay. Now, we can create a cluster. So, we can create create a lot of clusters for even one project, but we will need only one cluster for this project. Here you can see they have different plans. We will click on the free one. And so, we have a lot of regions here. And you have to select the region which is the closest to you. So, I will select Mumbai and then I will create this deployment. Once you do that, you get this window in which you can get a username and a password for your database. So, for every kind of cluster, you will have a username and a password because nobody should be able to access your database. So, this is the password to it. When I click on this button, I can copy this and then I get this to create this database user, which is me. I will click on this and then I can choose a connection method. We have a lot of connection methods and I will choose compass here. When I choose that, I get a connection string. So, this is an URI which we will be writing in our server. For that, I will go to the folder in VS Code, open it, and then we will make a new file named as .env. But what does this .env file do? So, we have to create a .env file. What does this file do? This file keeps all your environment variables, basically all the secrets of your project. So, these are the things which you don't want to push on your GitHub profile and you don't want to show people because earlier I said that you don't need anybody to access your database. So, this is where we can safely put our things like the database name, username, passwords, and all those things. For that, I will write here MongoDB URI. And you must be wondering what is an URI? So, URI is like the older brother of URLs. URIs are uniform resource identifiers. These are just like URLs. You know what a URL looks like, but these URIs have much much more packed in them. So, basically you have the database name, you have username, passwords, all in this one string. You can see that here. So, this whole thing is the string which I will copy from this button >> [snorts] >> and then I can paste it here. You can see this thing is the database name. And here I have to put the password. Now, here is the database access button. You can see who are the people who have the access to our database. So, we have one user, which is me, and I am the admin because I created this. So, when I click on this edit button, I can see this window where you can edit the password. So, we have to write a new password. Let's just write intro to backend. I have written intro to backend. I can show that to you and then copy it. Now, you have to update the user because we changed the password. And they are deploying it, so we have to wait for some seconds. Till then, we should be writing the password. So, I will cut this and paste in the password. Now that I have pasted it, let's save the file and let's make let's move on to the another file which we need to make, which is constants.js. So, this is the file where you will be keeping all the constants. So, it becomes easy for us to use in our server. So, what can be the constants like? Okay, first of all, let's make a source folder in which we will be keeping all our files. Then I will make another folder which will be config [snorts] and then in this I will make a new file named as constants. constants.js. And let's write export Okay, one thing we can see here is that we cannot write ES modules here. So, we have two kind of modules ways in which we can write this. For that, I have to come here in the package.json file and write the type in which I'll be writing all my JavaScript modules, which will be the module type. So, we have two types. One is common.js and the another one is ESM. So, we will be using the ESM one because they are the new ones and they are the standard way to write JavaScript modules these days. I will save this file and then we can go back to constants.js and then write export const DB name equals to whatever your database name is. Our database name was intro. Exactly. This was our database name. So, this file is not that important, but it's a very good practice to have a file like this because now you can see that I can just write DB name anywhere in our server, in our backend code, and it will point to this intro to backend. So, I don't have to write intro to backend everywhere. I can just write DB name and the server will know that what DB name is actually. Now, the another file which we need to make is app.js. We have to set up our Express app. Express.js is the framework we are using in this project. And app.js is the file where all your requests come firstly. So, we will understand this later also. Now, let's make the app.js file. I will make this in the source folder. app.js Now, the first thing is that you need to import the Express app in app.js file. So, import Express from Express. Yes, this is the syntax how we write it. Now, you can see that it's not coming in the color. It's not it's coming dull. Because we have not installed Express. For that, you can write you can open your terminal and then write npm install Express. So, this is the way you can install anything. When I enter this takes a bit of time and then you can see it has added all the packages and all. When you go on to your package.json file you can see that we have dependencies and it's saying Express and then the version of Express which we have on our computer now. Something called as package-lock.json file has also been created. We don't have to worry about this, but you can see that we have this number here. Why? Because we got a node modules file also folder and you wouldn't want node modules to, you know, pushed on your GitHub. For that, we need to make another file called a dot gitignore file. For that I will make new file named as dot gitignore. Now, the dot gitignore file is a file which in which you write all the things which you don't want on GitHub to be pushed. So, as I told you that you don't want your secrets to be pushed on GitHub. And also, you don't want node modules to be pushed on GitHub. For that, we can go to any browser and write that we want a dot gitignore generator. You can get a lot of generators. I will use this one. Here you have to write node. And then you will just click on create. You get a full file here. You can copy it like this. Copy. And then paste it here. So, what did we just do now? Here you uh I can show you. Okay, see. Here we have written node modules. So, now node modules won't be pushed on GitHub. Similarly, you can see .env, .env.test, .env.production. These kind of files are not going to be pushed on GitHub. I got the dot gitignore file out of the source folder into the backend folder because backend has node modules and gitignore easily would be able to ignore node modules. Now, we don't have those 600 things, but only five which need to be pushed on GitHub. Let's get back to our point where we were which was creating the app.js file. So in app.js we wrote this and we installed Express, but it's still not in color because we have not used this yet in our code. So let's write const app. We are making a app. And it is Express parenthesis. Now, you can see that it just got colored because I used Express. So, if you don't use anything, it will always be dull. Then, the last thing which you need to do here is to export this because we will be using this app in our other another files also. So, you need to export this. Export default. Okay, this way you >> [snorts] >> export this. Now, we have made our app.js folder and let's write a comment here. Create an Express app. Okay. The next folder which is our database.js. So, this is a very important file. It connects your backend to the database. Now, for this, we will be making a new folder in config new file named as database. js Now we have to import some stuff. Again import Mongoose from Mongoose. So, you must be thinking what is Mongoose. You need something to talk to MongoDB because we need something between our server and the database which will be talking to the database. And Mongoose is what does it. Also, Mongoose lets us define the structure of the data types which we will be making in the models in the later of the video. So, for now, what you need to understand is that Mongoose is something which talks to the database and it's important when you are using MongoDB. Now, open your terminal and again, we have to npm i Mongoose. Also, one thing that if you want to write install, you can write that, but if you just want to write npm i and the dependency name, it just works similar. So, npm i Mongoose, enter. And then you will see that Mongoose will get installed onto your computer. Now I will make a const and I will write the constant as connect DB because we are connecting to database in this folder and I will write async an arrow function. Now, you must be wondering what is async. Now, async await is something which we use when we want in JavaScript, when you want some task to finish and then only the next task should begin. So, this is a very good thing which async and await does. And this ensures that when one task finishes, then only the other task should begin. And it is very important thing because if we don't use async await, things will go messy and we won't be able to handle one task and then another. So, that's why we will use async await. Another thing which we will be using here is try and catch method. So, when you click on this, you get this. Try and catch is also a very good thing in JavaScript where you can just write the process which you want. And catch you you can catch your error. So, if the app crashes, you can catch the error specifically and this is a very good thing to catch all the errors which are coming. And this is a good practice because if any error comes, you get to know it. Now, let's write a const and then the connection instance. We are connecting to the database. Okay. So, I have written await here and Mongoose will do its thing and we will be connecting to MongoDB. And this line here says that we will be using .env the MongoDB URI which we just wrote in the .env file. So, it will be extracted from dot env file and then for ourselves, we will write a console log. So, then in the console log, we can write a sentence for us which will tell us that yes, our MongoDB has connected. We have connected to the database. For that, let's write a console log and I will use backticks because after the sentence, I want the host to be written. So, I want the address of the connection instance also. For that, I will use backticks and then we will write our line. You can write your own line. I will write MongoDB uh MongoDB connected. Yes, let's write this. MongoDB connected and then three exclamation marks. Then I will go on to the another line. We will use a dollar sign and then curly brackets and then I can just write connection instance dot connection dot host. So, this line will tell me the address of the connection instance. We will see that in the terminal in some minutes. And then it is completed. Now, the another thing which is very important, that's why we use try and catch, is that I will write a console log here too which will tell us about the error if any comes. So, I can just write MongoDB connection failed. And then it is an error, so error also when you we are using await that's why I will write process.exit. And one. So, this will ensure that when I when this whole process gets done this process gets exit and then we move on to the another task. Lastly, we will write uh export because that is important to use this connect DB constant this connect DB constant in our other files. So, I will export connect DB. I will save this file and then we can go on to the another file which is the last file which is index.js and the most important file. So, so far now we have made like these many files and I can say that if you want to write this database.js and app.js all into the index.js file, you can do that. So, index.js is the file where you start your server. This is the file which starts your backend service, your backend server. So, let's make the file in the source folder a new file which will be named as index.js. In this file, we will write how our server is going to start. For that, we will import First thing is that we need the stuff from .env file in this folder. So, for that I will import .env from .env. Now, what is .env? .env is a dependency which again helps us to extract environment variables. This lets us use our environment variables in all our server. So, we can Again, we will write npm i .env. Okay. So, it is going to install. Now, I can write .env.config parentheses, curly braces and then we you write the path. So, how will .env know that from which file do you need your environment variables? For that, we can write dot {slash} dot env. So, you can see that we have our .env in our backend folder. So, we write it as dot {slash} and then dot env the file's name. This is how .env is going to know that what do you want from which file do you want your env variables? environment variables. Also, here is an important thing. You need to give this thing here because it tells it to stop. Basically. If you don't, that's okay, but it's a nice practice. Then we have to make another constant which is going to be start server. Basically, this whole file is about starting our server, so I will name it as start server. Okay. Again, I will be using async and then an arrow function. Again, I will use try and catch. Now, I will write await and connect DB. Now, you may remember that connect DB was Okay, so I just clicked on that suggestion you saw and it got this import line from there. So, I imported connect DB from the config folder which was database. Here one thing is missing that I should be writing database.js because that is how the server will know to get the file. And let's continue it from here. I will add parentheses and then the next line. So, app is now going to be on. We are going to switch on our server. And then you write error for any errors. And then again, we will use uh arrow function. And then I can just write a console log because if any error comes, we need a line which tells us that yes, there is an error. And then you just throw error. Also, here I missed one thing. In this error, we need to wrap this up in another parenthesis. And and then a parenthesis here. Okay. Now, another thing is that you have switched on the app. Now, you want the app to listen. So, you write app.listen and then the process.env dot port. Now, what is the port? Like app is going to listen, but from which port? The ports are like you know that your laptop has a lot of USB ports to input stuff. And similar to that, online in the digital world, we also have a lot of ports from which your application is going to hear. So, your application is only going to use one port and that port can be specified in the .env folder. Like I can just come here in the .env file and then I can write port equals to like let's say I give it 4,000. And then I save this and then I go on to index.js and then I write here process.env.port. So, from the env folder, we are going to take out this port from which we are going to connect, from which our server is going to listen and then as an or sign, we can assign like 8,000. So, if by any reason this port doesn't work or this port is busy somewhere we will use by default 8,000 for to you know, just not get errors and easily do make the app listen. Now, we write arrow function and then again, we will write a console.log on whatever port our app listens, we get to know that. So, we can use backticks. We can write server is running on port whatever port you're listening. So, we don't know which port we will be listening. So, I can just write dollar sign and then the process.env.port. So, now we know that this port is going to come from the .env folder, from the .env file and it will be written in the terminal when we will connect to our database. Now, we need to catch our errors also, so let's write again in a log a console.log and then we can write DB connection failed. And then it's an error, so I will write error. And lastly, here I won't be exporting anything. We will just write start server, parentheses and yes. Save this file and now we have our server will start. No more files to write for our database. Now, one thing which we need to do is go here and in the package.json file, the scripts are what you write on your terminal to start your server. So, basically I will cut this line and write a script so that we can use that script in our terminal as a command to start our server and see if the database is connected or not. For that, we can write start and here I can write node source index.js. Why did I write source and then index.js because we have index.js in the source folder. And now, when I save this, I can go on to the terminal and write npm run dev. Okay, it's saying we have a missing script as dev and yes, it's right. Another script which is important to write is the dev script. Here I will write nodemon. Same source index.js. Now where from where did this nodemon thing came? Nodemon is something which we can install from our NPM packages. NPM I nodemon. Why do we need nodemon? We need nodemon because if you want to start your server, like let's say you are the developer and you are developing some back-end project. Now you want to start your server again and again and for that you will need some automation and that automation is nodemon. Because if you want to do it manually, you have will have to always click control plus C to terminate your terminal every time to start the server again and that is painful. So we use nodemon, it's automated. What it does is that when you install this and in write it as a dev script, what happens is that whenever you save your files, it automatically starts your server which saves a lot of time. So let's install this. And now I will save this file and now when you write NPM run dev, then we run we can run it nicely, but I think we are getting some errors. It's saying cannot find module. Now we can start our server, so I will go to the terminal. Now we can start our server, so I will go to the terminal and then write NPM run dev. So NPM run dev is the dev script which we wrote here to start our server and the start script also can start our server by writing NPM start. But we will go with NPM run dev and then I will click enter. So when I click enter, our server will try connecting to MongoDB, but I see that we got some error. So let's see. It says expected connection string to start with this or this. So if I go into my ENV file, I can see that our connection string is starting with this, so it is correct. That means that we have another error with this. One thing also I see here is that .env is not in the root directory and it needs to be in the .env directory, so I will drag it down to the root directory. Now I also see that we made mistakenly two node modules files which are not correct. We need only one node modules folder. So I will delete this one and I will keep this one in the root directory as it is. Also, whenever you change .env files, you have to start the server again, so I will delete this one and write NPM run dev. So it connected, but then it failed and we have an error named as app is not defined at start server. Okay, so in the index.js file, we have start server and it is saying that app.js was not defined, so app is not defined. So I can see that I have not imported app, so I will import app. I can go with the suggestion and now we have imported app because we were using app.on and then app.listen, so we needed to import it. Now I will save the file and let's see if it gets connected and yes, MongoDB has connected. The second thing which we see is here is the address of the connection instance of our MongoDB cluster and the next thing is server is running at port 4000. So this is how we get it that MongoDB has connected successfully and we can move ahead with building our server. Now we should be able to start writing APIs. So we will write authentication APIs and then basic CRUD APIs. So for the initial ones, we will be writing the login, register and logout APIs which are our authentication APIs. For that, I will be showing you this ER diagram. But first of all, we need to understand what APIs are. So APIs are basically application programming interfaces. But this is not how you can understand what APIs are, so we will build them. They have a structure. So if you see basic APIs like the authentication ones, they have routes, controllers and they have a structure basically, some models also. So the first thing which we need to learn is what a model is. And this is an ER diagram, but first of all, let's understand what is a model. A model is the code version of the structure of the data you want in your website. So how you want to structure every kind of data on your website is what models are. They define schemas and schemas are basically structures. For instance, you can see this is an ER diagram. What we can see is that we have users and we have post. So in our project for this video, we have only two kind of things. One is user model and another one is the post model. User model is for the authentication. So whenever you authenticate on any website, you go and you write your email, password, username and then you can register there. So this is for that and the post one is for the characters you can post on the website which we are going to make. Like you can post characters which will have a name, a description, age and this created and updated at will be written in the models. We will write them. So yes, this is basically the ER diagram. What it basically shows is that what kind of data exists in our system and another thing is how they relate to each other. So we have only two kind of data in our back-end which is the users and the posts. So now we can start building our models. For that, we will come back to the VS code and then make a new folder in the source folder named as models. Now in this models folder, we can make a new file named as user. model.js. You can name it as user.js also, but so that we can if we write it as like this, we can easily find it out that this is the model for users. Now let's start writing the code for this. We will start with Mongoose because Mongoose is what will talk to MongoDB for us. So let's import Mongoose. Import Mongoose. This has a different syntax when you write models. Schema is structure, so don't get confused on it. Schema means structure from Mongoose. Now let's start making our user schema, user structure which we are making here. Now the thing which I showed you which was the ER diagram, which we will use that only to write this model file. What are models? Models are the code versions of the structure of your data, which I told earlier. So this is only going to be the code version, so we can hop on to those diagrams and get the code in this file from there. Let's see how we do that. Now let's say first thing was username. You can see username is here. So I'm starting with username. I will write it as like this and then curly braces. Now the first thing is what kind of type it is. Type is written here which is string. So we can just write string here. Next thing is that is it required like is it a very required field or we can not write it if it is optional or not. No, it's not optional. Username is very important, so we keep it as true. Then the next thing is should it be unique? Yes, it should be unique. So we will write true also here. Then it should be lower case also. So we will again write true here. And then you can literally write a lot of things whichever you want for your username. Also, we can write trim. So trim is used for the white spaces. So let's say I will write a comment here. Let's say you are writing your username, okay? You write your username as Lily and then you give some white space and then you enter. So what happens is white space means that if I give space here, so this space is called as white space. And these white spaces gets trimmed when you make this trim as true. Now, the next thing which can be is min length. This is very important because you wouldn't be wanting somebody to write a hundred character username in your website. So, you would want it as like let's keep it to 10. And okay, let's keep it to one only because it's minimum length. Next is max length. Okay, max length is something we can write from our own choices. We can keep it as 30 as of now and then let's hop on to the another thing which is email or password, anything. Let's write for password now. Password, again curly braces, then the type. Type is string, so we will write string. Then is it required? Yes, it is required. So, we will write true. And then we will write min length as six. Yes, we can write it six or we can write it eight. Then max length can be a lot, but I will just keep it at 50 as of now. Now, let's write email. And again curly braces, then it's type. Type is string. Again. Then, I think we have these things again here, so we can just copy and paste it. Like it is required, yes, it should be unique, email should be unique. It should be lower case, it should get trimmed. And yes, so we can copy this much from here. And then write it as it is here. Okay. Another thing is that you can have a match like for emails, you just cannot go like this because emails should be in the email format. For that, you can add some stuff here, but for now, we will just leave it empty. Okay. Now, another thing which you might be seeing is that this created at. So, this is the date time when the user was created. Now, to get this, we will write another another one here which will be timestamps. If you write it as true, you will always get timestamps of every user when the user was created. So, it's a really good thing. And the last thing which we should do do is to export. We should be always exporting stuff, always exporting the constant which we take so that we can use it in another files. So, we made a const user, so let's write it and then this is the syntax for this. So, user schema. So, what is happening here is that Mongoose is talking to MongoDB and then from the model, your user is being extracted. So, we can use it in all our servers just by writing this user. So, this user is the user model. This user is not every normal user, but this is the user model because you can see it has a big U here which means it is the user model. Okay. Now that we have written the model, we can hop on to another sides of our APIs which is routes. Okay. What are routes? Routes are the addresses. It's like an address book. So, whenever somebody requests on something on your website, that request needs a path to go on. For example, if I go to any website like Y Combinator. If I go on to this website, I see a lot of stuff here. If I click on, let's say library. So, when I click on library, I have chosen that route. When I click on this URL, you can see it has library here. Y Combinator.com and then {slash} library because I am on on that library route. If I go on to companies, let's say, then you can see the route changed and it says company now. So, whenever I clicked on this library or this company, I requested this route. That's why this route came. Now, let's write routes for our register model. Okay, so this was our authentication model, this was our user model and now I want the users to register on my website. For that, I'll be writing a new folder in the source folder named as routes. Now, in this routes, I will make a new file named as user.route.js. So, this is the particular file for the users. Now, again let's start with importing stuff. We will import router here. Router is a tool which helps us get routes in Express. So, let's write Express here. Express is our framework which basically handles the routes, but router is the tool which we need to use. Another thing is that we will make a constant which will be router. And then equals to router. And then you export it. So, export default route. This is it for the routes folder. Now, let's make a controller. Now, we will make a controller. Let's go into the source folder, make a new folder named as controllers. Now, in this controllers folder, I will make a new file named as user.controller .js. Now, routes was where you handled the paths. So, whenever a request came, it came to routes and routes take it to the path where it needs to go, to the specific path, to the specific route which the request needs to go on. The next step is where controllers come in. Controllers are the things which really decide what kind of response has to be go on to the request. So, basically they are the decision makers. They handle what routes has come to them. Con- controllers are the decision makers. They handle the requests basically. Now, let's write the controller for our register model. We need to register a user, so for that we are going to write a controller named as register user. For this, we will import user first of all because we are doing everything for users here, so we will import the user model here. You can see I'm using this capital U user because this is the user model from user.model .js. You can see that I am importing the user model from models folder and the users.model.js file. Now, let's make a constant named as register user. You can make a constant named as anything, but whatever the constant does, that name is the best we can keep for it. Again, I will use async await here. Async await is nothing but something which finishes one task and then proceeds to another. It doesn't mess up things. Also, here I am going to use this. And then an arrow function. Now, why did I write request and response here because the first task is to see the request and the another one is the response. So, this is how it is the flow. Again, we will use try and catch method. As I told you, try and catch is the best method we can use to catch errors when any errors come to our application. Now, we make a constant. This constant is going to have username, our constant is going to have username, email and password. These are the three things it will ask for from the user. And then we keep it in the request.body because our request will contain all these things. Okay, then there are some steps. There are basically a lot of steps if you want in when you want to register a user. I will have some basic steps like a basic validation. So, a basic validation for what? So, basically if when I'm asking the user to type their username, email, and password, and they don't type any of them, then I won't be registering them. So, for that, I need a if statement which will check if they wrote everything which we want in our request body or not. Like, if there is no username or if there is no email then password, then we will give an error which will be response. We will give a response which will be a straight 400. Because it is it's not an error, but basically it's like a response where we give where we tell the user that all the fields are required. Like, we can write that as message and then write all fields are important. We can give a response like this if none of them or maybe even one of them is not being given by the user. The next validation or something like a check we can do is if the user already exists in our system. If the user already exists in our system, we cannot register them again and again. We won't be doing that. So, for that, we will check if the user exists already. For this, we can just make another constant named as existing and then await write await here and then from the user model, we will get we will find this user if there is someone named exactly same to this one. You can find the user by email or by the username. I will be finding the user by their email. So, let's write email dot to lower case. We want Actually, every email is going to be in lower case, but still we are writing it like this so that every email we find we are finding are in lower case. Now that when we find it after it, I will just write down if statement saying existing if the user is existing, I will return a response with a status code which is going to be 400 and now you just write a message here like our message is going to be user already exists. Okay, now we have done all our validations, two of them. Now we can create a user. Now let's assume that the user gave us all the three things, username, email, password, and they do not exist already. So, now we need to create the user. And for that, we will make a const named as user. And now this time you can see that I'm not writing a a capital letter U, but a small U because a small U names one user, but the capital U means the whole user model. So, we need to understand the difference between them. And then I will write await for the tasks to finish. And then user.create. Now the model user is going to create a user. Now in this, we will just write all the things which are given to us like username. And basically these things are going to be saved like this only and you will see them in Postman when we will test our APIs. So email will be in lower case. If it is not, then it will be converted into lower case. Password and then one thing more which is if they are logged in No, they are not. So, I will just false this one. Now the another thing is that if we need to catch the error. So, we will catch the error by this. Now whenever an error comes, like we need to give status codes. Again, we will always get get different status codes whatever type of the error is, according to that. So, if there is an internal server error. So, basically nothing is wrong on the user side, but on our server, we have some internal server error. So, for that, those kind of errors, we'll be writing here a message which will be a internal server error. And then it's an error, so we can write this is an error message. Also, we forgot to write the response for when we create a user successfully, so I will write here res.status and the status code is going to be 201 because we have successfully created a user and then we write JSON. And then we write a message which will say that yes user registered successfully. So I can just write here user registered. And then I want the ID of the user in my response also. So, I will write user and then their ID. So, I want to extract the ID, the unique ID given by MongoDB of the user document. And then I want their email also, so I will write email user.email. And then the username also. So, I will write username and then user.username. And that's it. So these are the things like the user ID, the user email, the username are going to come in the response when we create a user successfully. And the last thing is that we need to export the constants we made here. So, as we made register user here to use it in another files, I need to export it from this. So, I will write register user and then save it. Now we can check First of all, let's just check that if our MongoDB database is connected or not. So, I will write npm run dev. And I can see that yes, it's connected. So, that's a good thing. Now the next thing which we should be doing is that I will tell you about this. How a request flows through an app. So, this is basically the journey of a request which we just did. So, whenever a request comes on your website, your backend takes it. app.js is the first file which receives the request. One thing is that we need to write here is we need routes to be imported here. So, I will write it as import from routes and then user.route.js. And the next thing which we will do is to declare the routes. So, this will be routes declaration. It will also look like app.use. So, basically we are using the application now which we made the Express application and then we can just write uh like API version one and users. So, you must be thinking why did I write API here and version one. You may not write this, but for the basics, I'll just write this is the API, this is the version one, and it's users. So whenever we write this, we have to write the name also. So, this is the user router which we are writing for. And yes. So, this was important. Now what happens is another thing which I want to tell you guys, what is going to be the example route. So, basically the route I showed you on the Y Combinator website was something like an URL. So, what is going to be our URL? Let's write that also. It is going to be on localhost because we are developing on our local machine and our port was 4000 which you guys may remember API V1 and then users and then register. So, this is going to be our URL for now. Also, we have to parse the JSON requests we get from the users on our website. So, for that we need to write app.use and then express .json. So, what does this thing even do? Now, this express.json is something which gives the ability to our server to parse the JSON request it gets from the client side. Now, we were talking about the journey of a request. Now, from app.js we receive the request. Every request which comes to your backend server always goes on the app.js file and then it sends all the request to the routes files. Routes files checks the address, the URL path, and method. Method can be get, post, which we'll we'll be talking about further in the video. And then routes, when they handle all the paths and they send all the request to the specific controllers for them, then we get to the controllers and they does the task like fetching, saving, or deleting, whatever the user wants to do from the request. That happens from the controllers. Now, let's take a look on this flow in VS Code. So, when I go to VS Code, we can see the first file which where we get the request is app.js. So, we go on to app.js. We have written everything, but let me just show you the flow, which is here you import your routes. Like you have imported this file, which is the user.route.js, and then you have used this. So, here we have this users path. This users path, which is going to have all the routes for the user model. So, whatever you are writing in this controller file, this user.controller file, all of them will be under this one. Users route or users path. Now, the next file is routes. Okay, so in user.routes, we have to import register user, register user from our controller file. So, I will click on this and we will get this line. And then we have to write router. post and then parenthesis. / Now, here is you Here you write the constant name. So, in the user.controller.js file, we have this register user constant. So, I'm going to write that here. But, not that exactly. The path we want it to be. So, I can write the name here, which I want to keep for this particular path, which is register. And then .post. Why am I writing post here and here? Because post is the HTTP method we'll be using here. I will tell you about these methods soon. And here you write register user. Now, this route file is completed because I have written the route here. So, what happens is that when a request has come into app.js, it will go into the users path and then it will be directed to this register path inside the users. And after that, we will have the controller who will send the response to the request. And we have written everything here also. Okay. Now, let's save the routes file and then first of all, let's see if our MongoDB is connected. We have some error, which is cannot find module user.controller. Okay, so this is a wrong thing because here it should be user.controller.js imported from user.route.js. Okay, in this in this file user.route.js, you can see here I have not written .js. So, this was the mistake. Let's see if it worked. Okay, now we have another error named as cannot find module and then user.model.js imported from user.controller.js. Okay, let's see. Here, it is saying that it cannot find the module users.model.js. So, let's see what's wrong here. Users.model.js. It cannot find this file. So, in models we have user and I have written here users. So, this is wrong. So, let's cut the S and now we can see. Okay, now we have another error. Here it is saying that type error. It It has a type error. Argument handle handler is required. It means that in the route [snorts] file says that as post. So, here is some mistake and I can see So, it may be one from authentication. Okay. Now, we have HTTP methods. What are HTTP methods? These are the methods in which your request go. So, these are like tags on every request you send to the server, it has a tag. Or say it has a HTTP method. So, if it has a get tag, that means that you want to get some data. Get can be that when you open a social media website, let's say Twitter, you want to get a set of tweets on your screen, on your feed. So, at that time your request is in the get HTTP method. But, when you want to post something, when you want to create data on a website, you want to post a post. So, at that time you use this post. Also, when you are registering as a user, you are creating data on that website because you are registering as a user. Also, when you login, when you logout, all those requests go into post because you change some data or you create a data on the website. Now, we have put, which means you want to update the whole thing. Like, let's say you are you want to replace your user's profile, so you do that. If you want to update a part of your profile on a website, you use patch. And the delete one is to delete data. We will see this in the Postman app after this. Now, the another thing which we can talk about is HTTP status codes. So, the HTTP status codes, we have a lot of them and you can see the table is a bit big, but you don't have to memorize every and each of them. We have some means like 200, which means that yes, your request was successful. 201 means that yes, you created some data. So, if you logged in, if you registered as a user, you created some data, and you will get 201 or 201. The another thing is 400, which means a bad request that says a wrong request or maybe you gave some invalid input. So, they were asking for a username, but you gave an email on their input field. So, in these kind of things, you will get the 400. That means wrong request. You must have seen 404, which is the most famous status code of all times, and we all know it, which means not found. So, if you have requested for some data, and it doesn't exist, so you will see 404. That means nothing here at that address. The last one can be 500, which we talked about earlier in the video, which was internal server error. Where your server messed up. So, it's not the user's fault here, it's the server's fault, server messed up. Now, let's hop on to Postman to test our APIs. So, I will open this Postman app in my laptop. You guys can go on to the browser, and then you can access it there also. So, from this button, I will create a new workspace, and this will be a blank workspace. So, we will just click on next. After that, you have to name it. I will name it as intro to backend. And then I will just create it. So, this is the kind of window you'll be getting after you create a workspace in Postman. But, first of all, we need to know what Postman actually is. So, Postman is an application for API development and API testing, for which we are going to use it. We will create a collection here, and you will get this window. Here, we have to name this collection. I will name this connection as auth, because we are making auth APIs like register, login, and sign out. Oh, sorry, log log out. So, for those, I will write it as auth. And then, I will click on this plus button to make requests. Now, you can see that we have a request window, where you have you can see we have a lot of HTTP methods, and we can here enter the URL. So, first of all, just name it. We will name it as register, because this is our register API. And then, I will go on to VS Code, and I will go on to it app.js. Here is the example route or the URL I wrote down here, so I will just copy it, and then paste it over here. So, let's dissect this part. Here is the HTTP, which is the method. Uh also, here we need to select this post method, because we are generating data on the website. We are creating data on the website. So, that's why we are you going to use the post method. After that, our server is running on localhost 4000. This is how Postman is going to talk to our server and get the code. Then, API v1 users. Now, this part this part this part I have written in app.js. You can see here routes declaration. In routes declaration, I have written this for the whole user model. So, whenever I will be using this, this will mean that my request is going to this users model or coming from it, and then after it, you can see I have this register word here. Which means from the route register, it is going to go on to the path of register, and then the controller will handle register request. Now, there is something called as headers. Headers can be a lot of things, but the one we are using here is the content type. So, the content type is going to be JSON. The next thing is the body. So, the request.body, which is taken from the user. So, when a user is trying to register on your website, they want to give some data like username, email, and password, which we wrote in our controller. For that, we have a lot of options here like form data, and raw, binary, GraphQL. You have to select this raw, and that too in JSON. So, I will select JSON, and then I will write the data here. Let's assume that I am the user, and I have come to your website, and I want to register. Now, I am asked my username. So, I will write that. I can write Shivani here, and then this will also come like this, and then a comma. Another thing is the email. And then again, the email can be shivani@gmail.com. After that, the third thing is the password. I will just write it as 1 2 3 4 5 6. Okay. Now that I have saved it, then I will click on send, and when you send it, you get a 201 created. So, I sent it to my server, and it got me this. This says that the message is user registered. User has an ID, which is a unique ID given by MongoDB, then the email, and then the username. Also, you can see this is the status code, which says a new resource was created successfully. So, this is how you get a response on Postman without even needing a front end to check how your backend is working. And that's why Postman is a great tool. Now, the next thing, which is to show you guys how this has gone into our database. For that, I will take you to MongoDB. When you go on to your own project, that is intro to backend, which I made, you can see we have a button here named as browse collections. You click on this, and you get to a window where all your data is actually located. So, now we are going to see how data is actually written on MongoDB Atlas in documents. Now, you can see here is a sample of what MongoDB gives us to look on to, but we will be testing. We will click on this, and then users. So, you can see it's loading the documents, and we have one document here. This document has an ID. So, every document also has its own ID, and then the username, which was Shivani, the password, and the email. And created at time and updated at time. And the this so, underscore underscore v is the version, which is zero, of course, of this document. Now, you can see that this is how data actually gets stored on MongoDB Atlas. Now, the next API which we want to make is the login API. So, your user has registered, but if they want to come back to your website, they need to login. And for that, we are going to make another API named as login. For that, first of all, you won't be making any new model for this, because the user model is going to have all these APIs where the user registers, where the user logins or log out. So, these are the things where you don't need another models. We can just use this model only for this. Now, I will go to the user.controller.js, and here I will make a new function named as const login user. Same, we will use async, then request, response, and then an arrow function, and then try and catch. Now, the first validation which we need to do is that if the user already exists. So, I will write it as checking if the user already exists. For this, I will make a new constant, and then parentheses, sorry, curly brackets. We need two things to login. So, when you want a user to login on your website, you want them to give some data, because you just cannot let anybody login on to your website. So, they need to give the email and the password. You can have the username also here to let them put, but I will just have email and password here, and then I will just keep it as the request.body, because this is the body of the request we want. Now, I will write const user. We will take a constant named as user, and then await, and then the the user model, which has a letter capital letter U on it to find the user. Because we want to find if that user already exist or not. If it If they don't exist, then we just have to give an error message that, "Hey, you don't exist on our website or on our system." Also, uh user.findone is used to actually find from the user model or anything. And the dot here means that you want to extract that thing from user. Now, in this I can just write email because I want to find the user from their email. Also, this to lower case means that I want the email to be in lower case. And then parentheses. And here we can write a semicolon. Okay. The next thing is, what if the user doesn't exist? If the user doesn't exist, then for that I can write this. And then we can just return a response, which will have a status which will have a status 400 because we couldn't find the user. We can write here 404 also. That's your call. I am writing 400 here. And then the message. The message you want to give to the user, which is user not found. Because we couldn't find the user on our system. The next thing is a semicolon here. And now we are done if the user already exist. If they do, then we go on to the next level. And if they don't, then we just give them a response, which will be an error response because we couldn't find the user. The next thing which we want to validate is to compare the passwords. So now, let's assume that a user came on your website and they want to log in, but their password was incorrect. So you must have seen that when you don't write your password correct, you cannot log in. Why? Because the server was checking. It's checking if your current password matches the password you're writing. So for that, we need to do that kind of thing here also. We need to compare both the passwords. And for that, I will go to the user model. Now, I could have written this whole thing in the controller also, but I want to make a universal thing in the user model only so that I can use it and it can work for all the functions in our user model controller. For that, we have a dependency named as bcrypt. So what does bcrypt actually do? Bcrypt is like it can hash your passwords, it can compare your passwords, and it's a really good thing. So we need to import it. But we need to install it also. So first of all, I will just write this quick. And then we can hop on to our terminal. I can terminate this and then write npm i bcrypt. When I enter, I get that this has installed. And I have written the import statement here also. Now, I will just write how we can use bcrypt. Okay. So I will just write a comment here before saving any password, we need to hash it. So you must have seen in the MongoDB that the password is visible. Now, this is not a very good practice. This is actually not a good practice at all because what if your database leaks? And if your database leaks, then the illegal person, whoever took your database, can see that all the passwords are not hashed and they can easily open anybody's account and log in onto your website, which is a bad thing. So we need to hash it. When we hash it, nobody can ever find the real passwords. For that, I will just write here user schema. That means the user structure and then pre. Now, pre means that I am going to save the passwords, but before saving them, I have to hash them. So for that before thing, we need to use pre here. And then async function because it's in function and the next. Okay. And then you just write next because we need to move on to the next task after hashing the password. And then parentheses. Then there is one catch here. So what happens is whenever you are going to save a password, when registering a user, the thing is that you don't have to hash it every time the user logs into your website or registers. For that, I will write an if statement, which will be this.modified. This.ismodified. Let's write it as like that. So what means by modified is if your password is not modified. This statement means that if your password is not modified, it is not going to hash again. If it gets modified, if it gets updated, then we will hash it. So I will write the password here, the password word, which means that if the password was not modified, it is not going to be hashed again and again. Then I will return next again. Parentheses. Semicolon. And then I will write the normal statement, which means the this.password equals to await. And now, if this if statement comes true, then the password is of course not going to be hashed. But if this statement, this 40 number statement, if your password was actually modified, then your password is going to be hashed by bcrypt. So await bcrypt.hash because we're going to hash from bcrypt here. And then I will write this.password. And now is interesting thing. I wrote 10 here. Why did I write 10 here? Because you see we have salt rounds. Like whenever you're hashing any text or any number, you can set the salt rounds it needs to be hashed in. I have set it to 10. So 10 salt rounds will be there to hash the password. You can increase it or decrease it, but I guess 10 is enough and it's great the final next so that the other tasks resume after this. Okay, so I got this wrong here, I guess. Here is the parenthesis which will come. Yes. Now it's good to go. Now the next thing was that we wanted to compare our passwords. So to compare our passwords, compare passwords. Now I will use again user schema. And then this time we are going to use methods because we are going to compare our password. And then equals to again async function. Password. And then parentheses. Then again return. So now, this is a method where you don't need to write any if statement because there is nothing to do like that. You just want to compare the passwords and then we are going to just return await. And I will let bcrypt compare the passwords. Okay. So now this is done. I will save this file. And then we can go back to our user controller. Now, we were here. So I will write compare passwords. Now, you must be thinking why am I writing here also and then in that file also about comparing passwords. So what we wrote in the user model was like a base. So from there, I am just going to extract that thing here so that we can use it in login function. So I will write constant isMatch. So I'm going to match both the passwords, so I'm writing it as isMatch. We can write anything here. Then I will await the user compare password. And then parentheses. Again, we are doing this with the password, so that and then here also you are going to use a if statement, of course, because if it doesn't match, then you want to tell the user that, "Hey, your passwords didn't match, so we cannot log in you." So if the password was not in match, not a match, then you just write return. And then you write a response, which is going to be 400 because they gave invalid credentials. So we will write a message here as invalid credentials. Okay, after this what if the passwords match? And your if statement if it did not come true and the passwords really matched then you need to give a another response which will be 200 because the passwords were correct and you have successfully logged in. So, for that we can write a message here user logged in. After this we can write user because we want some data of the user like the ID and their email. So, I will write it as dot _id and then their email. And also their username. We want all these data when we get the response. And then we have catch. So, here I'm just going to write a response dot status which will have 500 that must go for our internal server error. message as internal server error. Okay, let's save this file. Then the next thing is that you need to export login user. Then let's go to our routes file user routes and then we will I will paste it and then name it as login. And HTTP method is going to be same as post because again we are creating some data on the website. We are logging in. And I will name it as login user and you can see it automatically came here but if it doesn't then you can just write it. So, we imported login user from the user controller file and then we make a route here for it and I guess we have not left anything. This should work. Let's first see if our MongoDB is connected. I will do npm run dev and we have some error. Okay, so it says the request module user controller.js does not provide export for this. But I just wrote an export like here. Let's save the file I guess. Yeah, so after saving it connected. So, always remind yourself to save the files. So, we are successfully connected. I guess we are good to go to go to Postman and check if this API is working. So, let's go to Postman and then make a new request. We will name it as login. Then the HTTP method is going to be post. The URL I can copy this URL from here and just put the URL here. We will just change this into login because this is the login one. We are still in the users model. So, that's it. Then the headers will be the same. The content type is going to be application/json. The body. Okay, so the actually what body are you asking is email and password from the user. So, you want to draw you have selected JSON then you have to write the email which was Shivani at the rate gmail.com and then the other thing was the password. So, let's write the password. The password was 1 2 3 4 5 6. I save this by the save button. Now, let's click on the button send. And then we see that our user got logged in. Here is the ID and email and the username. So, this is how our login API will work. Also, one thing when we log in a user that means that we are giving that user a access token and a refresh token. That means we are creating a session for them for how long they need to be logged in on our website. So, it can be like 1 day or 15 minutes or half an hour. It can be any time you set. So, if you set like 15 minutes after 15 minutes they will get logged out by itself. So, they will have to log in again. So, this is where you create tokens and you do that through JSON web tokens but we did not do that in this video. The next thing is that we need to make another API which is going to be the logout API. So, if the user really wants to log out from your system by themselves they want to hit this API. For that we will make another constant named as logout user. Then again we will use async and then an arrow function curly braces and then we will start writing a try and catch and now we will start writing the request body which we want from the user. For the logout one you don't need a lot of things from the user. You can only get their email and log out them. So, this is going to be our request.body for this logout part and the next thing which we will write is again the constant user await and then we need to find the user because if we cannot find the user how will we log him or her out. So, for that I will write find one and then we will do this by their email. So, the email they provide us will be the email from which we can find that user. I will just write email here and a semicolon and again if we cannot find the user for that I'm going to use an if statement which will be if we couldn't find the user then we will give a message uh basically a response which will tell the user that hey we couldn't find your ID or user not found. So, for that we will give a status which will be 404 404 and dot JSON then the message which will be user not found. Then a semicolon here. After this if we can find the user then that's a really good thing. We will give a status as 200 because it was okay it was clear then dot JSON and then a clear return response message that logout successful. And then we will write a error which will be 500 for internal server errors. So, the message will be internal server error. And it's an error so I will write error here. So, we are done with this API also. Here a semicolon will come and then save this file. Now, we need to export this constant also right logout user here. And then the route file I will go and paste this line and write logout here. And this is also going to be a post because we are again creating some data on the website because we are logging out from there. So, the website will be terminating the session they gave us at the time of login. Now, here I will write logout user and then save it. Also, when you save it here you can see that we have got logout user by itself and yes, I think we are good to go to check it on Postman. So, I will go on to Postman and then will make a new request named as logout. Then it is going to be a post method. I will copy the URL from here and I will name it as logout. After that we will give the headers which are going to be the content type that is application /json and in the body we are going to choose raw which will be a in JSON and then we will give the email which is Shivani at the rate gmail.com. So, we have logged in here and now I'm going to log out from the same email. So, let's save it and then send. You can see that we got in 200 okay here which says that logout was successful. And this is how our authentication APIs are made. So, this was the most basic way of making these authentication APIs. Now, I will just commit on GitHub. So, I will save all the files and write a message named as feature add auth APIs which were register login and logout. Okay, so we can commit now. Now, that we have made the authentication APIs, we can move on to make our CRUD APIs, c r u d CRUD APIs. We will make a new model file for posts. We which we will name as post post.model.js. Now, one thing is that we need to go on to this diagram where we made the users and the post models. So, we are going to look at this table or this diagram and then we will make the post model file. Okay, let's import first of all Mongoose because Mongoose is what we will use to talk to MongoDB and create our schemas, which are also known as structures. Then this is the syntax for this. We write schema and that from Mongoose. After this, we will make a normal constant post schema equals to a new schema parentheses and then curly brackets. One more thing which I want to tell here is that after these curly brackets, we can make another curly brackets which will have timestamps and which will be true because we want the created at and updated at dates here. So, this is how the whole thing looks like and the last line is where you export things. So, export const post equals to Mongoose.model because this is the model which we are talking about here and then parentheses post and then post schema. This is how a model file looks like exactly. Now, in this part, we fill all the parts from this diagram the such as name, description, and age. One more thing that we are not going to have a lot of input fields for our character posts. We will only have name, description, and age to understand CRUD APIs for now. So, the first thing is the name. So, I will write it as name and then type which will be string as written in the diagram. Name is in string, then a comma, then another thing is if it is required and yes, it is required because without a name, how can you post a character? The another thing is that we will use trim here. So, we are using trim to cut all the white spaces anybody could write in the name of the character. So, I will write true here and then we will hop on to the next thing which is the description. So, we will have one one-liner descriptions for every character on our website. So, this is how we will write description. Description is also a string because it will be normal sentences. So, it will be the type string. Then a comma, then required, yes, it is required. Then a comma and again we can write trim here because we don't want any unwanted white spaces. So, I will write true here. Then we have the last thing which is age. So, we can write age here also. For age age is not going to be a string because age is going to be an integer which we can see here. So, for that we are writing type and then you can write integer here also. I will write number and then is it required? Yes, it is required. So, I will write true. Then age can have maximum and minimum. So, we will write minimum one and then max can go up till like let's say 150. Okay. So, after this, we are done with this post model file. I will save this. And now, the another thing which we will write is the post controller. So, now why are we not writing this post thing also in the user controller? We can write it and it won't crack, it won't crash, but for the readability, we will write another file which will be post .controller.js. So, now in this controller, all the routes of posts will come and the controller will be deciding what kind of responses has to go on those requests which are coming for posts. So, this is how you can manage your code files nicely and increase readability. Okay, so for this file, we are going to write we will first import the post model because we are going to use that here. So, I will write post and then from from the models file folder and the post model.js. After this, the first thing which we are going to write is to create a post. CRUD has C in its first place which means create a post. So, I will write create a post. For this, we are going to make a constant named as create post. And then we will be using async here also and then a response, then an arrow function. We will be using try and catch as we always do. And then I will make another constant which will be the request body which we want from the user to create a post. To create a post, they need to write a name, a description and a and the age of that character. After that, we will just write request.body. These are the things we want in the body of request. After this, I will write a normal if-else statement because what if the user doesn't write a name but writes the description and age? We cannot register that post. So, I will write name and then description and then age. So, what I'm specifying here is that I want all these three things or we cannot post. So, if either one of them is not provided by the user, we will give them an error which will be res.status 400 because they have given us invalid fields. So, I will write it as a message message like all fields are required. So, this is the message the user will see and they will then know that yes, all the fields are required. Then what if the user gave us all the fields? So, at that point, you will have to create the post. So, I will write it as const.post because we are going to create the post now. We will use await and then the post model.create. See. And then I will normally use parentheses and then curly brackets and write name, description, and then age. Now, we have to give a response also. So, it will be a 201 response because something has been created on the website and our backend server will give this response and with the message named as post created successfully. Now, for the catch error, I will write res.status 500.json. And you know what message we are going to give here which will be internal server error. So, internal server error. Now, we will write a normal export for this file. Here, I will write create post. And yes, that's it. The controller has been made. Now, the next thing is that we want to make a group file. I will again make a new file for the post routes to maintain the readability in our code files. So, I will make a new post route.js file. Now, in here, you need to import router first of all. So, I will import router and then from express. Then I will import the create post controller. So, I will write Okay, this is the option I'm getting. So, I will just click on it and the whole line comes here. So, it is getting the create post from the post controller.js file. Okay, we are good to go now. Now, it is normal like the one we did in the authentication APIs. You just make a constant router and then you write it like this. Then on the next line, you just write the route to your create post model. Sorry, create post route. So, you write it as router.route and then we will call it as create for now. Then we are creating some data on the website. So, we will use the HTTP method post here. Also, this goes in these. Okay. Now you write the constant name here which is create post and this is it. Then in the app.js file, here also you need to make new routes. Till now, we were only using this user router, but now that we have made another post router, so we need to import that also here, which is going to be post router from dot routes folder from post route.js. And then I will declare the route also here as app.use and then it will be the same API V1. Now here in the place of users, I will write posts and and yes, it is named as post router, so I will name it as post router. Now I think we are good to go to test this API of creating a post on our website on Postman. So I will go on to Postman and from here I will click on this plus button to create a new collection, which will be a blank collection. You click on this and then you get to a new collection. We will name it as CRUD because we are making the CRUD APIs in this. Then I will click on this plus button and then I will get to a new request. This request could be named as create post. Okay. The method is going to be post because we are creating data on the website and the URL is going to be pretty same like let's copy this one and then come back here and then just cut till the users and write posts and create. Okay. Now for the headers how do you know what kind of headers we want to use? You can just go to the post controller and see what kind of request.body we are taking from the user. So name, description and age. We use the content type header, which will go with application/json. The server is going to pass that. Then in the body, I will select raw, which will be into JSON and I will just write a name, which can be let's say Queen and then basically I'm making a character, so I need to write the name description and description can be a martial arts artist. Okay, and then I have age. Let's write the age, let's say 26. So I will just save it and send it. Okay, the request model does not provide an export. Oh, so in the routes folder, I did not write a export. Yeah. So I will write export default router. Now I think the server should start. Yes, the server has started. Now one more thing which I noticed was that in the post controller file, we are getting some error here, that's why I am seeing these in faded. So okay, so this is the bracket I think I used wrong. Okay, so now we are good to go. Now let's go on to Postman and send it. So now we can see that we have a message, which is post created successfully. The post has a name, a description and age and then it has its own ID, then created at, updated at because we wrote timestamps in the model and version is zero of this document. Now that we have created a post, we can keep creating posts as in we can make another post named as lemon. So this person is a soul person and its age is 150, which was the limit we set it as the max one and now save it and send it. So now we have created a character named lemon and you can just keep on creating this but we will make our another API now, which is to get all the posts. So think of it like you're on Instagram and you want to just get your feed. To get your feed, you don't need to give any request.body. You don't need to give give anything. You just need to get all the posts. To get all the post, we are going to make another API in the same post controller. So let's write another one which will be named as const get posts. Write it as read all posts. So now in this also we are going to use async, so I will just copy this and paste it here. Then inside this I will write again try and catch const get posts then again I will use await here and then we are going to show the user. We need to find all the posts and for that I will just write find here, which will find all the posts which are uploaded on our website and then we will give a nice response for it which will be a good 200 response and that will be in JSON and then we will write posts because that is our constant here and then we will again write Okay, then we will write the error of catch. So I can just copy this because we are writing the same error one after another, so I will just write it here as the Okay. So I have written this. Now we can save it. Okay, so we are good to go. Now let me just separate these. Okay. Now the next thing is that you go on to the routes folder and then you again copy this paste this here and write get posts. This is going to be a get method, so I will change the method from post to get get here and I will write the name as get posts because that was the name of our constant. Actually, let me check again. Yes, this was it. So okay, here in the constant, I had to write posts and now I will export it. Get posts and now I think we are good to go and we can we have made the routes also, so we can test this on Postman now. So let's go on to Postman, create a new request, name it as get all posts and we will Okay, so the HTTP method is going to be the get one only. Now I will just use this one and then in the place of create, we will write the one we wrote in our code, which is in the routes folder, get posts. So I am going to write this. Let's copy it and write this here. Now what about the headers? So headers are used when you are like if you are using the app content type header, which is to pass into JSON, but we are not giving any data. So no data has to be passed, so we are not going to use a header here. A header which could be used here was the authorization header in which you would give the application your access token so that it can authorize you and show you all posts, but we are not doing that here and in the body also we don't need any body. So I will just save it and send it. Oh, we need to start our server first. Wait a minute. Okay, so we have some error here. Get posts is is not defined. It says in this in the post routes folder the Okay, so here in this import file, I have not imported get posts, which is correct. So yes, so now I think the server started. So we can now send it. Okay, so it gives an error. Check how if I have written the wrong URL, but I think it's right because it's post and then get post. Uh Oh. Huh, so when I copied this, I got post here but we need to have get here, so I think this was the only problem. Let's save and send it. Yes. So now you can see that we have got a list of all the posts we made. This was a test post which I made earlier. This is the Queen post and this is the lemon post which we just made. So this is how you get a list of all the posts you made. So this is how our get post API works. Now let's make another API, which is going to be the update post API in which we will be able to update our already made posts. So for that I will go to the post controller and then I will make a new function named as const update post. Again, we will be using async here and then request response arrow function then we will start writing our validations. So, we won't have a lot of validations here, only one, which is that the user is not giving us empty data. So, for that, I'm going to write uh first of all, let's write This is the basic validation for to to check if the body is empty. Because we don't want the user to give us empty bodies and then our API is handling them. So, this is the most basic validation we'll be writing here. So, if statement and then object.keys. So, what actually is this object.keys? Now, let's write a bit here also that this is for our request.body. Now, what does this do? So, our request.body is a string. And let's see here how this works. So, basically, what is happening is that this kind of like see, if we have three input fields, name, description, and age. So, the name was anything, let's say it is X, and then the description is Y. And then the age is Z. So, this is the string we have. Now, the thing happening is here that we want to calculate the length of this thing. If the length is zero, that means the given data from the user is zero, and which is equal to empty, which we won't be going ahead with. So, this is the kind of string data we are having in request.body. Object.keys is something which converts this thing into an array. An array of keys. So, it takes out the keys from this string. Keys are This is the key, and this X is its value. So, these are key-value pairs. So, we will just get the keys from here like name, description, and age. So, these are only the keys. Now, these will be counted as fields. So, this is field number one, two, and three. And if the field number the field length is equal to zero, that means that our given body is empty, and we will give an error message that you need to give all these things to update your post. Now, I will write here {dot}length, which is equal equals to zero. So, if it is equal to zero, that means we will give and return response.status, which will be a 400 because the data was not provided, which we need. So, the message will be no data provided for update. So, this is the kind of message we can write here. And then semicolons here. Also, one thing which you must be thinking here is that why can't we just keep the request.body and let this string be calculated? So, the thing here is that if you know, in JavaScript, if we get this kind of curly braces, which are empty, it is known as truthy values, which is not reliable because if this comes as a truthy value, you won't be getting a good response, which will be empty, which we want here. So, that's why we don't use this because JavaScript behaves differently with the strings and arrays, and that's why this thing is called as truthy value. So, we won't be using this. Okay. Now, the next thing is that what if the user [snorts] gave us the values? So, for that, we will be updating their post. And for that, I will write here const post equals to await, and then the big post, which is our post model. And we will find the post. We need to find the post to update it. So, to find the post, we need to write find by ID and then update it. Here also, we need to write and update. And then the usual will come, request. params. This is also a new thing here. Because the ID of the post will be taken out from the parameter URL, from where you created the post. So, we will be seeing this thing in Postman just after some time. So, yes, and then request.body. And then there is also a new thing, which is new equals to true. So, now that we have updated the post, we need the new post, which will have the new values, and I will put it as true because we need the new one, the new post, which has been updated. Now also, another if-else statement here should be that what if we couldn't find the post? So, for that, I will write if we couldn't find the post, we will return a response as res.status, which will be 404 because it was not found. And {dot}json parenthesis, and then we will give a normal message as post not found, and then semicolon here. After that, if our post was created, we found the post, and we updated it. So, now we need to give a 200 response, which will be that yes, your post was updated. So, I will write it as JSON, and then parenthesis, curly brackets, and the message will be post updated successfully. And this [clears throat] is a post, so I will write post here. And yes, we are done with this. Now, the next and last thing is that we will catch an error here. So, for that, I can just copy the above error because this always stays the same. I will copy this and paste it over here. Okay. Now, we can write that post couldn't be updated also, but but we can just write internal server error because it is on the server's fault. And then the next thing is that we need to export it also. Okay, so there is some problem with this. Let's see where it is not coming right. So, okay, now it is correct. So, we need to export it. I will just copy the name from here. So, let's copy it, and then paste it here. Save the file, and then we will go to the routes folder, which is here. I will copy the same line and paste it here. And we can write here as update. I will only write update here because we can write the full constant name here here. Yes, like this. So, when I clicked enter, I can see that update post came in this import line also. So, that's a good thing. Now, one more thing that here we have the HTTP method, which we need to use. This is clearly not a get method one request, but it's a patch one. So, we have put and patch as HTTP methods to update data. Now, patch one is for updating some of the data, and put is for updating the whole data. So, we will use patch here, and this is more reliable also. The next thing is that update will have an ID because we need to find the particular post by its ID by its unique ID to update it. So, here we will put that post's ID also. Okay, let's save this, and our server is running fine. And then we will go to Postman. We will create a new request named as update post. Then, [snorts] I can just copy the URL. Like, we can go for this, and then in the place of create, I will write update because we have update here, and then update we have an ID here. So, ID will come like this. But, the ID shouldn't be written like this. We need to really get the unique ID here and put it here. So, when we created the post of this lemon one, we got an unique ID here. So, I will just copy this, and then go here, and then in the place of ID, I will put it. Then, I will change this method to patch because we are updating the some of the data, and then I can go into the headers. I have this content type, which is application/json, already. And then in the body, also we got this. So, let's just change it a bit. In the place of lemon, let's write lily. So, lily is a flower. And I can write here a cute flower. Okay. Age huh Age can be two. And then we will save it. And then we will send it. So, we got another error. Okay, so in this error, we can see that object ID failed for value. It's saying that it's not complete. So, let's go here and see. Okay, so I did not copy it fully. This six was left, so let's copy it again. And copy it and then go into here and again write it. So, now I have it full six here also. Now again save it and send it. Okay, now we have got 200 okay. That means post was updated successfully and then its name was changed to Lily a cute flower is two and then these timestamps version zero. You can see the ID is still the same because the document is still the same. You just change its values. So, this is how it is. And now we can just go on to VS code and make our last API for this video which is going to be the delete API. So, now what I want to do is as a user want to delete this whole post. For that I will go on to the post controller and make a new function named as const delete post. So, now we want to delete the post and for that I will always like always times I will use async. Then the same request response and then an arrow function. In it I will use try and catch. And then here, okay, so in delete post you don't need to have a lot of validations and I'm not writing any here. We will just go on to our constant which is deleted. And then I will write await here. Then from the post model I'll be finding the ID. So, find by ID and delete now. So, this time it is going to be delete. And then I will write request.params because from the parameter URL of the post ID I will get its unique ID. So, that is it and then we will return a good response which will be okay for first of all, before that we need to write an if statement also because what if you couldn't delete the post because you just couldn't find the post. So, it will be same as post not found which we also wrote in the in the update one. So, I will just write here deleted and then we will return a response as 404 because that is what says post not found. json and then a message which will say post not found. Okay, I forgot the quotes here. Okay. So, it is correct now. Post not found and then this I think something is wrong here. Uh Okay, so yes. Now I will return a response when we can just write response.status here which will be a 200 because we were successfully able to delete your post after we found the ID. If it was not found then you just get the 404 message the 404 response, but if it was found then you get a message that yes, your post post successfully deleted. Okay, after this a mandatory catch message which I will just copy from here and put it here. So, we are done with this. Now I will save the file. Uh our server is running very fine and then I will export this which is delete post. Then on the routes file I will go and again copy this and write it here. Now this time we are deleting it. So, I will just write delete and then again you need the ID of the post. So, I will have ID here as it is and then as the method it will be changed to delete. I will have to write delete post. You can see I have I'm getting this third option. I will click on this and it will get delete post in this import line. So, that's a good thing and then save this. And now I think we are good to go to test this on Postman. I will go on to Postman and then I will again create a new request named as delete post. Then I will have the URL. URL can be So, we want the update one. I will just change it this into delete. And then I think this is the same as one which we just updated. So, yes, this is the same. So, we will just keep it because we want to delete that lemon one post. We don't need any headers here because we are not giving any data which need to be passed. Also, we don't need any body here. So, I will just clean this. And then just save it and send it. Okay, so uh I did not change this patch into delete. So, let's change it this into delete and then save it again and send it. So, now you can see we got a 200 okay with a message post successfully deleted. So, by now we have made four APIs which were create post, get all post, update post and delete post and the another one were authentication APIs which were register login and logout. Now we can just go on VS code and we can push this all good code on GitHub. So, I will just write here that we also added some more features which were add CRUD. Okay, let's write it as like add CRUD APIs. So, this is it. I will commit it and then sync changes. So, yes, that was a wrap and in this video I hope that you guys got a basic understanding of what and how a backend looks like and works. That too with Postman where you can test your APIs and what are APIs like in the actual world. Now uh you can tinker with this and you can expand it very much. You can have a lot of validations. You can have indexes. You can generate tokens and you can do a lot of things. And that is what backend is which is the behind the scenes of a website. So, let me know in the comments how you are doing with backend and what you guys are trying and what you got to learn from this video. Thanks for watching.

Original Description

This introductory guide teaches you how to construct a basic backend for a website using popular technologies like Node.js, Express.js, and the MongoDB NoSQL database. The tutorial covers the core components of backend development, walking you through the initial server setup, database connection, and code structure using a model, route, and controller pattern. You will learn to build practical APIs for user authentication, including password hashing, as well as full CRUD (Create, Read, Update, Delete) operations to manage data. Finally, the video demonstrates how to utilize Postman to test your server's requests and ensure your APIs are functioning correctly. This course was developed by Shivani. She is part of Hack Club. Hack Club is a global non-profit organization that creates a community for high school students interested in coding and making things with technology. Hack Club is free and any teenager can join. Check out their site to get stickers and learn more: https://hackclub.com/ Resources: GitHub Repo Link: https://github.com/NewSmoke38/intro-to-backend Linkedin: https://www.linkedin.com/in/shivani-backend/ Notes: https://excalidraw.com/#json=MZ-nK2h_8IKWFL0Nm1gxH,QJrqIq4vpqwtzvjh4YMFIA ⭐️ Contents ⭐️ - 00:00 Introduction & Overview - 01:44 What is a Backend? - 02:01 Core Components: Languages, Databases, Runtimes, Frameworks - 04:36 Backend Architecture Flowchart - 05:44 How Frontend Connects to Backend (APIs) - 07:00 Prerequisites & Installing Node.js - 08:50 Project Folder Structure - 09:30 Project Initialization (Git & npm) - 12:56 Setting up MongoDB Atlas Database - 15:45 Environment Variables (.env) - 18:36 Constants & ES Modules Setup - 20:30 Creating the Express App (app.js) - 25:15 Connecting Database to Server (database.js) - 31:30 Server Entry Point (index.js) - 39:00 Setting up Nodemon & Running the Server - 44:00 Understanding Models & ER Diagrams - 46:26 Creating the User Model - 53:40 Understanding Routes - 55:00 Setti
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This tutorial teaches beginners how to build a basic backend for a website using Node.js, Express.js, and MongoDB, covering server setup, database connection, and API creation for user authentication and CRUD operations. By following this tutorial, learners can gain hands-on experience with backend development and create their own RESTful APIs. The tutorial also covers best practices for server configuration, database management, and API testing.

Key Takeaways
  1. Install Node.js and set up a new project
  2. Create a MongoDB Atlas database and connect to it
  3. Set up environment variables and constants
  4. Create an Express app and connect to the database
  5. Define models and routes for user authentication and CRUD operations
  6. Test APIs using Postman
💡 Using a model, route, and controller pattern can help keep backend code organized and maintainable, making it easier to scale and modify the application as needed.

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →