Start Setting Boundaries With Confidence

Setting boundaries is key to creating a loving relationship in which your needs are met, you feel respected and valued, you feel a strong sense of self-love, and you feel safe.

Smartphone

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




Conclusion

There are many ways developers use to upload photos, storing them in a database as well as rendering them on the page. I will show you how to upload a single image using React, Node, Express, and Multer.

You’ll learn how to create a single-side basic React app, and along with that, you’ll also create API using node and express.

Steps we are going to take:

Enter the following commands in your terminal for installing the app and starting it.

Type one of the two following commands in the terminal, to install the routes library.

Have in mind that we are going to have two routes, one for displaying stored photos and one for uploading a single photo as well as inserting string data and sending it to the server.

Open the index.js file and update it as the following code in order to include the route library.

Open the app.js file and update it as the following code to include routes. We will create Navigation, AddItem, and Home components in the next sections.

In the src folder create a routes folder and file like this — /routes/navigation/navigation.component.jsx, and fill it with the following code.

We just created links for home and add-item routes.

In the routes directory create a file like this— /routes/home/home.component.jsx, and copy in it the following code.

We still didn’t make the server-side project, so these aren’t anything to fetch. For now, the home component will just return plain text.

In the routes directory create a file like this — routes/add-item/add-item-component.jsx and copy the following code.

Here we created input text fields, for the image name and image description as well as a file field for uploading an image. Later we will add an async function for saving the image to the database when we create a server-side app.

You can make a folder for both sides of projects and copy your react app there and create a folder for the server side, you can name it server.

Navigate to the server folder in the terminal and input the following commands. Here we created a package.json file and installed the next dependencies:

Express — A web application framework for Node.js.

MongoDB — NoSQL Database based on document and collection format.

CORSSupports — Access-Control-Allow-Origin CORS header.

bodyParser — A body parsing middleware for Node.js.

uuidSimple, — fast generation of RFC4122 UUIDS.

Multer — A Node.js middleware handles multipart/form-data

Create a src and models directory, and item.mongo.js file inside it, like this — src/models/items.mongo.js.

We created the items collection with images, names, and descriptions.

Create items.model.js file inside the models directory and copy the following code.

With this code we created a function for getting all objects from the Items collection as well as a function for saving the new object to the Items collection. As you know the model is responsible for communication with the database.

Create routes directory inside src directory and items.controller.js file in it — like this — src/routes/items.controller.js and copy the following code.

We created a function for taking requests to get all items as well as a function for saving items to the database.

Create items.router.js inside the routes directory and copy the following code.

We just created the items’ routes and defined the destination for saving images and the rule for filenames. In the route for saving data, we need middleware for uploading single images.

Create directory services and create a file like this — src/services/mongo.js. Copy the following code.

As you can see finally we are connecting our schema with the mongo database. You can easily create a node database on Atlas cloud and get the key and copy it instead on this default key: mongodb+srv://<- your API ->@clusterprojects.8wokfsp.mongodb.net/?retryWrites=true&w=majority.

Create app.js file inside the server directory and copy the following code.

Here we added all the needed middlewares which I mentioned before.

Create index.js file inside the server directory and copy the following code. Here we are defining and starting the server.

Let's use these created API endpoints in created React App.

Open add-item.component.js file and update it like the following. Install Axios library — npm install axios.

Here we created an async call to the server, for saving the object to the database. As you can see we didn’t send it in a JSON file, we used FormData, because of the image field included.

Now let's fetch all saved items and render them in the home component. Update your home.component.jsx file as the following.

Install Buffer library for react — npm install buffer.

We used the Buffer library to read buffer data and convert it to string, in order that we can put in the source attribute of the image tag.

Finally, we created both the server and client side of the project and updated the image, and save it in a Mongo database together with string data as well as we rendered all saved data on the home page.

Add a comment

Related posts:

Il codice etico di WWG

Il nostro codice etico racchiude tutti i principi in cui crediamo noi di WWG. Questi capisaldi comportamentali sono condivisi e vengono applicati da tutti i nostri collaboratori. Ognuno di noi…

Buy Twitter Comments

Twitter is one of the most popular social networking platforms on the internet. It has over 300 million active users and continues to grow. Twitter allows you to share your thoughts and ideas with…

How to Welcome Innovation Into Your Life

Innovation is a talent that everyone has, yet oftentimes, think they don’t. If you’ve ever marvelled at somebody’s creative prowess, guess what? You can create and innovate too. It just takes time…