How to Build a School Website with Strapi using Vanilla Javascript
Tags: javascript, strapi, html, webdev
Published: 2022-10-05 08:22:41

**Author: Dennis Muasya**

This tutorial will teach you to build the frontend and backend of a school website using the Strapi API, HTML, CSS, and Vanilla Javascript. There won't be any framework in it. Considering the target population, the design will be straightforward and uncluttered. I'll be talking about a school website I created using backend services in this blog post. I've previously worked with local JSON data, and it's challenging to update and delete the material. 

[Strapi is a free and open-source Headless CMS that allows for quick and easy API design and content management.](https://strapi.io) You will learn how to use vanilla JavaScript and Strapi CMS to create a school website.

## Prerequisites
Make sure your computer has the following installed before we start.
- **Node.js**: Due to the fact that NPM and Strapi are both built on Nodejs, this is the first tool you will require. Download it from this page. The needed NodeJS version ranges from 14.19.1 to 16.x.x.
- **NPM**: This is a package manager for Node. The Nodejs binary is included with it. NPM >= 6.x is the necessary version.
- **IDE**: Integrated Develepment Environment such as Microsoft Visual Studio Code, Android Studio, and Sublime Text.
- A basic understanding of asynchronous JavaScript.

## What is REST API?
The REST API, often known as a RESTful API, divides a transaction into several tiny components. Every module deals with a fundamental aspect of the transaction. Although this modularity offers developers a great deal of freedom, it might be difficult for developers to create a REST API from scratch. Several businesses already provide models for developers to use; the most well-liked models are those offered by Strapi. Commands are used by a RESTful API to access resources. A resource representation is the status of a resource at any particular timestamp.
 
The REST API in Strapi enables access to the various content types via API endpoints. When a content type is created using Strapi, API endpoints are automatically created. When making a query to an API endpoint, API parameters can be used to filter the results. There are no relations, media fields, components, or dynamic zones by default when using the REST API. Fill up particular fields by using the populate argument.

## How to Read DOCS
When you are familiar with the tool you want to use, the official documentation is the best place to start. When working with something new, repress the urge to jump right in and read the manual instead. You will also make time and effort savings as your skills as a developer advance. You can learn about many different subjects by reading. You will be better able to understand the next topic by knowing more about one area of software development.

Endeavor to go through each example in a *Getting Started* lesson. Before moving on, be sure you understand the foundational ideas of a new language or system.

Finally, familiarize yourself with the code. Reading the documentation alone probably won't be enough to understand what the program is doing. A little glance at the source code can have a significant impact. Utilize each and every piece of code by copying it, reading it, and determining what it does. Try out the code yourself to see how it works. Start it over again. Break it. then choose a resolution.

## How to Work With Strapi CMS
Use [this GitHub link](https://github.com/Dmuasya/SchoolWebsiteStrapi) to clone the school's website. The final step is to add the common JavaScript files in order to access the data from Strapi. To save the school website, decide where to put it on your computer. 

1. On your terminal, enter the following command:
```bash
    git clone https://github.com/Dmuasya/SchoolWebsiteStrapi.git
```
2. Change the directories to school website once the cloning is finished.
```bash
        cd SchoolWebsiteStrapi
```
3. Enter the following code into the terminal to construct a new Strapi app into a backend folder.
```bash
    npx create-strapi-app backend --quickstart
```
This results in the creation of a new Strapi app in a backend folder. The `—quickstart` flag instructs Strapi to use SQLite as its preferred database. When given the option to utilize a different database, omit the flag and select that database instead.

The length of time it takes to complete the installation will depend on how quickly your network is. 

5. After finishing that, run the next snippet to start Strapi in development mode.
```bash
    strapi develop
```
You should see your browser launch at http://localhost:1337/admin/auth/register. If not, manually launch your preferred browser. It will display a sign-up form similar to the one below.

![Strapi Dashboard Login](https://uc7a1acc7af1aee9030cda13f5f5.previews.dropboxusercontent.com/p/thumb/ABqCAUgelL1gqOgBq4OYvmSDxE_HcemHlzrKhk-dRDrz6RbWe7yqve1gFYnnC2i157PvAndx2e_7N24tc0mtRqz5G9DbMljDJC6zGw1NfgJQvBxen_Jdo7ZjuFlPPiPc20010h95SXjANSjWY33cIpkdy5ExX_A-E9Sx5HALah12ZBniMXxn36fsvoM_x9ZjG6buqGILBbvjiVyt47G6qfwhiEcmlSx-7vfRE_QeVqjlBug7rgQmeHmUPJqg8DRCOGZUasagqFeoggSW2mukCtuqWpaPJIh3Veqyjx2Jjm9878NJcAC2Z0RWJ8gmz3ETVubmQyfZPTvf7vj2tMN3b6ovSQQf5f_JJE2Gzt398sh0Y6HbKlXOFJPQPz8QE39_Rqs/p.png)

6. Now, enter your details in the form. This step is important for subsequent logins to the admin dashboard. Your dashboard should now appear like this:

![Strapi Welcome Page](https://ucff1f9a0b77efef0bfc80632824.previews.dropboxusercontent.com/p/thumb/ABpq8WDCJQ7MFGn-x-VH1OtyvLIWubc3Dd0FZkwiw2JCaLclE5x97hKa70a55XflPZ3xrz6WutBt3b-G_A02J8-EOm1rNNZJUStAcF-DKXxnP7JPjWtfTlV3N9zdUf7kZrPYWgbY90fcd8TJRJEZIQFJWj8ps1MdWJMxEGXvL5D-j8dWR5LcNtmKuAjdzP7LAxArNIXBZca4r4rLM-rFUBFsCzb2wiZTimPBK0h5O-nwyowbBrt-SXQhqBHmLPiTpHHVM5lD7nqHkR1F6shDs7byApYGvvHqVUVrHnMl0MUOjQy85JZfn4Yc1VoBz9sxVlAxXwwkewlPasRN7rx2Giwnr_nI9Z50sWqaPM8rPHgL54PmOL6pwUhP53HiMQjeXMI/p.png)

On this page, your APIs are listed in the upper-left corner. It should only contain the Users API for the time being.

7. To build the grade endpoint, choose "Content-Type Builder" from the sidebar. 
8. After being redirected, select "Create new collection type" and give it the display name "Grade". Despite the fact that the term is singular, Strapi pluralizes it by default. The modal screen should look like this:

![Strapi Modal Screen](https://ucf27bfc7d6dddcb5f3f563a7949.previews.dropboxusercontent.com/p/thumb/ABrotwEclGSZGHJxS77URL2fBggNXfOzgqUhJS_u03aw6f67WyJcNVs7ETsbFJ8kVJdB_TWE8SRUH6l0zPp3xZzNJcfrvy8gQOE4RvGhvzrhNv9j52b1DVD182Z8tMjSqPov8pQ8fUHw4aSZBzyO1XTzGsAN5JkhXa1EeHSwt8nn-StOMD5NLr1VpjovMSjopD8C65RLvxheVZDYTsDQ6uUsc3XnWR_MptV_ZtwAc-tWF_PW4YZWgLLHcqit9DEVxzU1kahdTFwj8M-iALLKmTvP8xnVhJjIJoq0DqBv2ObDYH4CvNo-0fMvE3rU2eP4B8MhIP5U6RnWsQM2IZKicVi4sQqU6IOYOycnafqmLwhjzqmeu3genmQRymZO3glUGB-Dwmy8yWnF3HTJoX5lod-H/p.png)

## Add New Grade
The fields for this collection can be chosen on a new screen that should appear after clicking "Continue." From the list, select the "Text" field, then give it a name. 

![Add New Grade](https://ucd9f2ce32bac9668dd25829a57e.previews.dropboxusercontent.com/p/thumb/ABr-Hq0ks-KhAao4ZWfjpPoP40h9bof5M0hoY2eVuqT6Z7f6zpdALVX7JsGtwI1SWcZEN7VYO3Nb8tHnR5C4KE3DC3AdN7flyXMbVv9tIO3PX3e0sLa46szJrV9NSwcLDz2HqMXqW0SiWUJzWwUZ77UY2jOoRZnib898XZK2UFlz8p7qeAYFDs20yf0TkHlwajQyUNZwfIeQe_P8ED0ROoloVWSJjC2Zp2nU_KbOt-K0oBSV8ldznEoT911oG79QNf6omhX6SpT9WDhqkRIzhzGrwZ00AyGuMTR0Vm1uA7hHbb-NGrla5xmaClHeCBETdNfQJ8PqHsxT19UtVYBRy6gulVkfE2Ijz9KL6Zf8WULl_Fx3h85kLMwN3z-8hThO_L3olGJsJ2OqFuvkvfbZH0dw/p.png)

When creating a new grade, make sure this information is compulsory by selecting "Advanced options" and checking the "Required field" box.

![Required Field](https://uc81d80c9174c11f141735768611.previews.dropboxusercontent.com/p/thumb/ABpL2Z6DmwebDRiLnoueLV5nId9RJj643OblH-VjCIVAJKGb0KalQngwosn5x4IOEinKvWLc66R218n5zHZJzf-bpyb4uz-PCjqipI12dgSmNBJmAf6m5AFgCLeN4fLd0V14HdkxSRd19LbO7aVEAZgUbnjMX6QO1Ha8vH_VTyLNX6dqj2G6UG-T8_Gz1eD42n7eptNDcYMj4vOExQaEJmrcaR5weQP-8DRPF_lBflPGq0KI1nZCd3aRLTc1dqRwJ-CT_vWpf1BYl5K1ek8iE2qMw0tvBF0QvLIqr0BOqZ22SChRMWRgf8RXPqnaL0opYbFtBwoQShMfMOLSxfkc9im1mbL44dzi9z96BLYKIelJA6b0JmDc6GL1AADjp7KQzRQ/p.png)

You’re done creating the grades endpoint.

![Endpoint](https://uc80404609f770682c2f3019b875.previews.dropboxusercontent.com/p/thumb/ABpM1qscRbvfl7F-b0zYlOG6bc8dIjn5rW4UsCq2x1fOY_JwiDYIGlpDM3xUEFmeuR-8TaHe4NNsWVRWL_nYvFejiB1KpSX0jIp3M-VaizK_BMEJiiMdlFIyJGcNqfjNkTP8hyADorxq8XH3SHEcEoIPQj5-ZecSdKBPrxBEpQPhkLNyRSQqXPkgWZYmwNGyZJA1WY0IGDsJKPzQ88myiPHlx0-WbJ2Ay9bvMl2TBBFsIiKWDNTqxtLYbX5oVpvF501T9RF-9YjdqLuXJ2f1d07TCaddbw_Du2RqOha1P3XSEkLJ4VwAnsdCP5lEpGT9Lc3b5brz7mvuVRUJRIBzHqStmEv6fU3yDByXuUrbF752Mg08t8JXIK2GQNN25GCuWV2767Ekng79Epp_el3zvWho/p.png)

## HTML Structure with Meaningful Classes 
HTML is a powerful tool that gives your code superpowers if you want to utilize it. The built-in behaviors in HTML allow you to reduce the amount of explicit code you need to utilize. By organizing your papers so that they work well and look good, you may make your content accessible to more people and software.

The project demystifies fundamental HTML tags and properties while instructing you on how to write semantic, meaningful HTML. It helps you ensure that your pages (including elements like navigation) are properly arranged and sectioned while also showing you how to use the language's unique features for presenting quotes, figures, dates, and timings.

By design, HTML is a tolerant language that permits the execution of even subpar code. Our school website won't render without components like <!DOCTYPE html>, <html lang="en">, <head>, and <body>. Certain browsers won't correctly display pages without these auxiliary elements.
```html
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>School Website - Strapi CMS</title>
   <link rel="stylesheet" href="css/minified/style.min.css">
   <script
   src="https://kit.fontawesome.com/fc31dedf63.js"
   crossorigin="anonymous"
 ></script>
</head>
<body>

 <!-- Modal on page load -->
 <div class="modal" id="modal-one" aria-hidden="true">
   <div class="modal-dialog">
       <div class="modal-header">
            <h2>Namaste !</h2>
<a href="#modal-one" class="btn-close" aria-hidden="true">×</a>

       </div>
       <div class="modal-body">
           <p>This is the Moi Forces Academy  Website but it's <br /> 
```

By following the few straightforward guidelines below, we can maintain our code well-organized.

- The indentation of nested elements is required.
- For elements, attributes, and values, lowercase is preferred.
- Use either single or double quote marks, but be sure you use them both consistently.
- It is recommended to ignore the Boolean attribute values for complete organization.

When we decide on a technique that we like. It must be adhered to strictly, applied everywhere, and kept separate.
```html
 <!-- acheivements section -->

    <section class="achieve">

      <!-- achieve container -->
      <div class="achieve__container">
        <h1 class="achieve__h1"></h1>
        <div class="achieve__section">
          <div class="achieve__sectionList">
            <img class="achieve__imgIcon" src="./img/ach1.png" alt="achieveIcon" />
            <h3 class="achieve__h3"></h3>
            <p class="achieve__p"></p>
          </div>

          <div class="achieve__sectionList">
            <img class="achieve__imgIcon1" src="./img/ach2.png" alt="achieveIcon" />
            <h3 class="achieve__h31"></h3>
            <p class="achieve__p1"></p>
          </div>

          <div class="achieve__sectionList">
            <img class="achieve__imgIcon" src="./img/ach3.png" alt="achieveIcon" />
            <h3 class="achieve__h32"></h3>
            <p class="achieve__p2"></p>
          </div>

          <div class="achieve__sectionList">
            <img class="achieve__imgIcon" src="./img/ach4.png" alt="achieveIcon" />
            <h3 class="achieve__h33"></h3>
            <p class="achieve__p3"></p>
          </div>

        </div>
      </div>
      <!-- End achieve container -->

    </section>

    <!-- End acheivements section -->
```

Last but not least, although inline styling first seems to be easier, it actually requires more upkeep and adds to the line of code. If we use an external stylesheet, we can choose to reuse our code. A single external stylesheet can also be used to style all of our pages.

## DOM in JS, ES6 Syntax, and ES6 Modules
The Document Object Model (DOM) represents the objects that make up the structure and content of a web document (DOM) as data. It illustrates how to use APIs to construct websites and web applications as well as how an HTML document is represented in memory.

The good news is that contemporary browsers have begun to provide module capability in native implementations. This can only be a good thing as it enables browsers to load modules more rapidly and effectively than they would be able to if they had to use a library and carry out all of the additional client-side processing and round trips.

```js
import { localhost } from "./../../adminUrl.js";

export const Blog = async () => {

  try {

      const url = `${localhost}/eventsblogs?_sort=id:DESC`;
      const ftch = await fetch(url);
      const data = await ftch.json();
      const result = data;
      //console.log(result[0].content);

  
      for (let i in result) {

          const container = document.querySelector(".blog__container");

          const dv = document.createElement("div");
          dv.classList = "blog__single";

          const img = document.createElement("img");
          img.classList = "blog__img"
          img.src= `${result[i].thumbnailimg.url}`;

         
          const heading = document.createElement("h1");
          heading.classList = "blog__heading";
          heading.textContent = `${result[i].title}`;

          const desc = document.createElement("p");
          desc.classList =" description";
          desc.textContent = `${result[i].description}`;

          const date = document.createElement("p");
          date.classList ="date";
          date.textContent = `${result[i].date}`;

         const hrf = document.createElement("a");
         hrf.classList = "below";
         hrf.textContent = "Read More Below:";
       

          const readmore = document.createElement("div");
          readmore.classList = "readmore";
          readmore.textContent = `${result[i].content}`;


          const hr = document.createElement("hr");
         
          dv.appendChild(img);
          dv.appendChild(heading);
          dv.appendChild(desc);
          dv.appendChild(date);
          dv.appendChild(hrf);
          dv.appendChild(readmore);
          dv.appendChild(hr);
        

          container.appendChild(dv);     
     
      }

  }
 
  catch(error) {
      console.log("failed", error);
  }
  }
```

The import and export declarations are necessary for the use of native JavaScript modules. Imagine yourself in a circumstance where you must reuse some JavaScript code. ES6's concept of modules comes to the rescue.

Each module corresponds to a corpus of JavaScript code. Modules contain both functions and variables. A module is nothing more than a segment of JavaScript code that has been written in a file. A module's internal variables and functions are not accessible by default. By exporting them from a module, variables and functions can be accessed from other files. In ES6, modules can only function in strict mode. This implies that any variables or functions defined in a module won't be globally accessible.

## Modules for SASS (SCSS) & BEM Convention
Sass is the world's most advanced, dependable, and effective CSS extension language. Thanks to the integration of internal Sass capabilities into the module system, we now have complete control over the global namespace. Before using, the built-in modules math, color, text, list, map, selector, and meta must be manually imported into a file.
```css
.blog {
    margin-top: 7rem;
    margin-left: 7rem;
    margin-right: 7rem;
    margin-bottom: 3rem;
}

.blog__h1 {
    // text-align: center;
    font-size: 2.3rem;
    margin-bottom: 5rem;
}

.blog__img {
    object-fit: cover;
    width: 40%;
    height: 230px;
    border-radius: 21px;
    margin-bottom: 2.5rem;
}

.blog__heading {
    margin-bottom: 1rem;
}

.description {
    max-width: 85ch;
    margin-bottom: 0.5rem;
}

.date {
    margin-bottom: 0.4rem;
    font-size: 1.3rem;
    color: blueviolet;
}
.below {
    // margin-bottom: 0.6rem;
    background-color: burlywood;
    color: #fff;
    padding: 0.2rem;
    font-size: 1rem;
    margin-top: 2rem;
}

.readmore {
    margin-top: 2rem;
    max-width: 97ch;
    color: #3a3838;
    word-spacing: 0.2rem;
    letter-spacing: 0.1rem;
    line-height: 1.7;
}

.blog__single {
    margin-top: 4rem;
}

@media all and (max-width: 768px) {
    .blog {
       
        margin-left: 1rem;
        margin-right: 1rem;
        margin-bottom: 3rem;
    }

    .blog__img {
        
        width: 100%;
        
    }
    
}
```

In computer science, there are just two difficult issues: invalidating caches and naming things. It is common knowledge that the use of a proper style guide can speed up development, debugging, and the integration of new features into legacy code. Unfortunately, the majority of CSS codebases are occasionally created with no organization or naming rules. Long term, this results in a CSS codebase that is unmaintainable.

The name Block Element Modifier is abbreviated as BEM. It guarantees that everyone involved in the creation of a website uses the same codebase and same language. You can become ready for changes to the website's appearance by using correct naming.

## School Website Design
Below is an image the design of the school website.

![School Website Design](https://paper-attachments.dropbox.com/s_FF75380B7FB91FA59BA004DB20CE8BC70363F97AB55AAA7AAD1612BD39309A97_1660214121063_school.PNG)

## Conclusion
You downloaded the Strapi app and created an API throughout this tutorial. Then you added sample grades to display and made a few routes of the API accessible. Last but not least, you copied a basic JavaScript School website and added scripts to fetch and show data from the Strapi app. Consider listing grades in the site and making the tags clickable if you want to give the app extra capabilities. I'll close by challenging you to take it a step further by including more fields in the API and turning on the fallback option so you can test how it functions when new data is added to the backend.

If you want to publish your school content, Strapi is an excellent option. In addition to supporting rich text, it also offers a user-friendly composition interface in the admin panel. The content can also be consumed by your school website via an API that is fully customizable. Check out this summary of Strapi's features on its website to find out more about what more it can provide. Finally,  [this application's source code is accessible on Github](https://github.com/Dmuasya/SchoolWebsiteStrapi).