Building Something From Scratch Phase at Encora Apprenticeship — Week 2

Ximena Sandoval
3 min readNov 22, 2021

--

This post is part of a weekly assignment at Encora Apprentice and in this series, I’ll share my journey as a Software Engineer Apprentice. I hope these stories help and motivate others on their tech journey.

Express and MongoDB

This week, I got to work on a small project with my mentor using Express and MongoDB. Last week I talked about how to connect Express with the database in a Docker container, but this week I worked with interacting and inserting records in that database.

In order to do so, once we are connected to the database, we could use the following to insert a new record:

/**
* Insert a new word to db
*/
app.post('/words', (req, res) => {
console.log(req.body);
words.insertOne(req.body)
.then(result => {
res.redirect('/');
})
.catch(error => console.error(error))
});

In the code snippet above, we are telling the server the endpoint ('/words'), and the callback (what to do).

We can also note that the callback has two arguments, the request, and the response objects. With the request, we can obtain the information we want to send to the database and with the response, we can send a response message or redirect to another page.

Also, note the words.insertOne(req.body) line. This is telling to insert one record into the database.

Using a Dockerfile

A Dockerfile is a file that documents the steps and commands used to build a Docker image. This week I got to work a little bit more with them while developing some scripts to download and install certain programs for different operating systems.

Here I describe some of the commands I used within a Dockerfile to help me with the task described before:

FROM base_imageRUN your_instruction_1 && \ 
your_instruction_2 && \
so_on
EXPOSE port_number_you_want_to_exposeCMD ["/bin/sh", "-c", "a command to run in bash"]ENTRYPOINT ["command to run when starting your container"]

Once we’ve defined our Dockerfile, we can then build our Docker image using

docker build -f /path/to/our/dockerfile .

React components

This week I also got to work with developing a new page and a new component of a React app. On the page, I specified the component I was going to use along with the props to pass.

<MyComponent prop1=prop1 prop2=prop2 ... />

The basic anatomy of a component is as follows:

const MyComponent = (props) => {
/* Get the props to use them later */
const { prop1 } = props;
const { prop2 } = props;
/* ... */

return (
/*Here we add what we want to render, it can be some HTML */
<h1>Hello {prop1}!!</h1>
)
};
/* To have it available from outside the file */
export default MyComponent;

Keeping your branch up to date

Finally, when working on a project where multiple features are being made in parallel by multiple people, it is important to keep your branch up to date to make sure to consider other code changes made by others. To do so, we can use the following commands:

git checkout main 
git pull
git checkout my_branch
git rebase main

And what they do is the following:

  1. Switch to the main branch.
  2. Pull the latest changes.
  3. Switch to the branch we are working on currently.
  4. Rebase our branch to have the latest code changes from the main one.

Final thoughts

This week was full of hands-on experience on working with the MERN Stack and also working with Docker images and writing bash scripts. I learned a lot about Express and how to work and interact with MongoDB, and working on a project with a team has been really interesting, since we get to discuss many points of view of how and what to do and help each other.

We are almost at the half of this phase, and I’m looking forward to keeping on working and learning more about the stack we’re using!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ximena Sandoval
Ximena Sandoval

Written by Ximena Sandoval

Cloud Platform Engineer at Encora. Currently learning about CI/CD, Kubernetes and watching Studio Ghibli movies✨

No responses yet

Write a response