Web Scraping Google With Rust

The Rust programming language was developed by Mozilla in 2010, as an alternative to C and C++. It is widely used for web and game development. In this tutorial, we will learn web scraping Google…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Integrating MongoDB With Express

Last week we started the series off by learning how to create our routes in Express, today we will be expanding on this by actually creating a custom route and create an API.

To get started we first need to add a dependency. In terminal make sure you are in the directory we are working in and do npm i mongoose. Mongoose is an Object Data Modeling (ODM) library for MongoDB. This is how we will interact with our database.

With our dependency now added inside our index.js we will add the following lines:

With this we have now created a database called subs. all thats left to do in index.js is to add a json parser. To do this, all we need to do is add the following line:

At this point we have fully set up mongoose and our database, and all that is left to do is create our model, and the custom route.

In order to create our model, let’s create a directory called models. This will hold all of the database models will will be creating. In this folder create a file called subscriber.js This will be our model we will be creating.

In this file the first thing to do is require mongoose as shown below:

With mongoose now in this file, we will now need to define our schema. We can do so with the following syntax:

A schema takes in an obj which has keys for diff properties for the object. In our case because this is a schema for subscribers we will want to do the following:

What this says is we are defining a subscriber with the attributes of name,subscribedToMagazine, subscriberDate inside each object we need to specify the datatype as shown above.

With our schema now defined we will finally need to export this module. However when exporting schemas, we need to do the following:

When exporting a model from mongoose, we need to pass 2 arguments, the name of the model and the name of the schema as shown above.

All that is left now is to create and define our subscriber route. Inside the src directory create another directory called routes and inside of this create a file called subs.js. Before we get started coding in subs.js we will need to add the following to our index.js

We have now connected our subs.js to our index.js.

Now within our subs.js we will need to do the following before we start

We can now start defining our routes as shown below:

Lets start with the get request for all the subscribers, we can do so the following way:

The reason we are doing try-catch is because if the route is not available we need tell the user of the error that is occurring. This will be a reoccurring theme throughout the definition of the rest of our routes.

Because we haven’t created and data that we can see with our get request, we will be creating our post route next as shown below:

With this post route now created, we can go into postman and do a post request and see we will receive our response both after the post request, and after the get request to localhost:3000/subscribers

Now before we work on the last three methods we will want to create a method to find our subscriber to keep our code as DRY as possible. This should look like the following:

Now lets tackle the get request for a single subscriber which should look like the following:

Going to a specific subscriber will now show you the data for the single subscriber.

Lets now create the delete route, which is also quite simple we will just need to do the following:

Notice how we are still utilizing the getSubscriber method and utilizing await.

Finally for patch, we will do the following:

First we need to check if the data we need is there and then set it to the parameters wi will be updating. After this is when we will use the try-catch in order to make sure we can save and if not we will want to send a response corresponding to the error.

With all this done we have officially created a simple CRUD API using express. Next week we will go into how to use a React frontend and complete a full MERN web app.

Add a comment

Related posts:

The Courage To Dream

There are two ways to bring your child up. One way is to teach them through fear. Tell them the harms of doing things a certain way. Tell them why they should not do what they are doing? Fear has…

Best of Self Publishing Guide

Hello writer! Here are some highlights from Self Publishing Guide this week. Thank you for your support!. “Best of Self Publishing Guide” is published by N. K. Carlson in Publishing Guide.

HOW TO RECOVER FINANCIALLY FROM CRYPTO FRAUD

Hackers broke into my wallet and stole over 40 bitcoins. Everything occurred so quickly. I was at work one morning when I received an email from blockchain asking me to verify my account once more…