Node.js Express Image Gallery Using MongoDB & Mongoose Library Full Project

You are currently viewing Node.js Express Image Gallery Using MongoDB & Mongoose Library Full Project

Welcome folks today in this blog post we will talk about how to make a image gallery in node.js express with the help of mongodb database and the mongoose library to connect to the mongodb database.The step by step youtube video is shown below just watch the video to follow with the blog. All the source code is given below copy and paste the code.

 

 

Requirements

 

 

Express will be the server on which the app will run on Port 5000

 

 

Multer will be the library used for uploading the images on the server

 

 

Express handlebars will be the template engine on which the app will run.

 

 

Mongoose will be the library to connect with the database that will be used for the application

 

 

Nodemon will be a dev dependency which will automatically restart the application when we make any kinds of changes in the application.

 

 

 

Now Make a App.js file that will be the starting point of the application

 

 

 

So in this block of code we are initializing a express application and then starting a new app. When someone goes to the home route then we return hello world as a response. We start the application using the listen method at port 5000

 

Initializing the HandleBars Template Engine in App

 

In order to use handlebars into your application you first of all need to create a directory structure such as first of all you need to create views directory in that directory you need to create layout directory like this

 

 

main.handlebars file

 

 

home.handlebars

 

 

Now Modify App.js to include the handlebars

 

 

Now if you execute the app on command prompt you will see the following output

 

 

 

 

Multer Image Upload

 

Now in this section we will be uploading image files using multer so first of all you need to create the public directory inside the root project and inside it you need to create a directory named images to hold the uploaded images such as

 

 

 

 

 

 

In this section of code we have successfully completed the operation of uploading images to the images folder

 

 

Handlebars code to show success message when image is uploaded

 

 

In this section of code we are connecting to mongodb and describing the schema of image file and exporting it.

 

Full Source Code

 

 

 

DOWNLOAD SOURCE CODE

 

Leave a Reply