visit
Because nowadays all Javascript technologies like React, Angular and Vue are very popular for building Web applications, so we can get an answer for the first question very fast.
But what about the back-end? Should I use NodeJS or .NET Core? Is it better to use a relational or non-relational database? Well, Strapi has the answer to all these questions.
First, we are going to set up and build our API.
Create a new project
npx create-strapi-app blog_api --quickstart
If we want to customize our application to use the specific database we need to run the command without
--quickstart
argument. By default, Strapi is using sqlite
.Once the setup from the command above is finished Strapi will automatically run (NOTE: when manually start the project run the command
strapi develop
) and we can navigate to our admin panel on the following link: . When you navigate you will able to see the registration form.First, what we need to do for our Blog Application is to define the models that we will have. We will define three models: Page, Post, and Content.
To create a new Model navigate to Content Type Builder.
Our model
Content
will have:Text
RichText
boolean
Media
Post
(Content belong to many Posts
)Page
(Content belong to many Pages
)Page
model will have:Text
Content
(Page
has many Contents
)Post
(Page
has many and belongs to many Posts
)and
Post
model will have:boolean
Page
( Post
has many and belongs to many Pages
)Contents
( Post
has many Contents
)As soon as we define our models we are ready to create some pages, contents, and posts. We can simply do that by navigating to each model and click
Add new [name-of-the-model]
Now, when we have models and data into our database we need to give access to our visitors of blog application. To do that we need to navigate to
Roles and Permissions
tab in the menu. We can see there are by default two types of roles: Public
and Authorized
. We navigate to Public
role and select:npm install -g @angular/cli
ng new blog-web
cd blog-web
ng serve
Now, we can start with styling our application and access data from our API. First, we will create services and API calls to get our data from Strapi. Navigate to
src
folder and run the following commands:mkdir services
cd services
ng generate service page
ng generate service post
ng generate service content
Angular CLI will create these services so we are ready for coding. In
environment.ts
we will put our API URL.We created two methods: one for getting all pages and one for getting page by id. We will make the same for
post
and content
services.NOTE: Before using
HttpClient
we need to register into app-module.ts
HttpClientModule
from @angular/common/http
,import { HttpClientModule } from '@angular/common/http'
Add it to the
@NgModule.imports
array.imports:[HttpClientModule, ...]
Next, we will create
post-component
that will contain style and functionality for posts and we will use app-component
for displaying our landing page. Navigate to app
folder and create a new folder called components. Here, we will store all components that we use in our blog application. With the following command we can generate a new component:ng generate component post
Because we are using bootstrap classes we need to include bootstrap into our project as well. We can do that by adding the following into
index.html
<link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootswatch/4.3.1/cosmo/bootstrap.min.css">
And we are almost done. The only thing that left is to modify
app-component
and our blog is ready for use.Also published on