- Keep motivated to learn.
Tools I Prefer:
- Code Editor I like to use Visual studio code.As this is the best editor I know. Let me know if you know any better options than this.
- Postman to test api endpoints.
First thing first we will navigate to the directory were we want to initialize our package.json. Once we navigate to the directory we are going to run this command to create package.json
npm init -y once we hit enter we will be able to see
package.json file created. Inside here we will create a directory name
app inside it create a file name
app.js This will look something like this. Later we will create all other directories as well. At the end this is how our file structure will look.
package.json inside scripts we will add two line one to run using
node and other running with
nodemon npm package. This will look something like this.
Once done with this we are going to install some of the packages that we are going to need. To install all packages we will run
npm command in our terminal. Like this
npm install -D nodemon Here -D stands for dev dependence.
We are going to add some dependences as well at start we will need to run these command to install
mongoose we are going to use
mongoDB for our database so we will use
moongoose Its a data modeling tool for
mongoDB database. We will run this line in our terminal.
npm install express body-parser mongoose
Once these steps are done we will move forward to our app.js file and initialize our server using. Here is how it will look after implementing all three above packages.
Here I am using
MongoDB Atlas this url here will connect us to mongodb database with database name
mern-app . I have also added post route on line 15. And imported
postRouter on line 2. I will create that file in a moment in routes directory.
After setting our
app.js First thing we are going to do is create a
Post model here is how the code looks like. Inside model we are going to provide all the field that we are going to save in database related to post.
After adding Post Model we will move forward inside Routes directory I will create a file name
postRoutes here we will have all the routes related with posts. Here it will look something like this after adding all 4 CRUD routes.
Here I have added one more package called
express-validator this help us to validate incoming data.
On line 2 I have imported postController we will create that in a moment. But First of all let look into
express-validator we have use body here from express validator and title should be length more than 5 and content should me more the 10 is being validated.
Now Here we look into the post controller where all the functionality stays. Let’s see the code first.
Here I have added functions for post image upload as you can see that in post create function. And For Image Upload we need to install one more package name
multer you can install it by running this command
npm install multer After installing this we need to add some additional settings in our
app.js Here you can see complete code below.
Here this is how you can create a curd api with express js. In the next post we will add authentication.