The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

Getting started with Strapi, create a first post and make it public

posted on 15.6.2023 by Below Surface in "Strapi"

The installation is pretty straight forward:

npx create-strapi-app@latest my-project

After the local installation, the backend is started on port 1337:

http://localhost:1337/admin

If you need to manually start the development server, run:

npm run develop

If you want to start coding on another system, the simplest way is to copy the whole directory, because of my current understanding, the created data of the backend lives within the folder and may not be pushed to GitHub (if you use it at all). To make the data to copy smaller, delete the node_modules folder(s) and after copying, run

npm i

To install all dependencies (node_modules).


For my test project I wanted to recreate the backend of this blog. So first I used the Content-Type-Builder of the admin page to create a Post collection. It includes the "Text" fields:

And the rich text field:

A date and draft field were not required, since Strapi adds something like that by itself.


Then I created a simple blog post to test the API: Easy. But what's the API endpoint to get this post? I searched for the Plugin "Roles & Permissions" and finally found it at Settings/Roles/Public/Post. I enabled "find" and "findOne" and there it was, displayed at the right side: The endpoint: /api/posts/:id (the /:id part is optional, for the findOne action). Don't forget to hit "Save".

All posts can now be received via the URL:

http://localhost:1337/api/posts

And the post with the id 1:

http://localhost:1337/api/posts/1

Output:

{
   "data": {
       "id": 1,
       "attributes": {
           "title": "How to initialize a new Angular project",
           "category": "Angular",
           "author": "Below Surface",
           "excerpt": "Starting from zero might be troubling, as it was for me as a React dev",
           "tags": "angular, initialize, start",
           "sources": null,
           "body": "Deleted the body for a better readability",
           "createdAt": "2023-06-15T16:12:51.785Z",
           "updatedAt": "2023-06-15T16:12:54.542Z",
           "publishedAt": "2023-06-15T16:12:54.539Z"
       }
   },
   "meta": {}
}

Amazing!


I have to say that this worked very well and the whole process - besides finding the API endpoints - was quick and intuitive. If I compare this to a WordPress installation, this feels like the future to me!

Tags:

strapi
node.js cms
cms

Sources:

https://strapi.io/https://www.youtube.com/watch?v=6FnwAbd2SDYhttps://forum.strapi.io/t/roles-and-permissions-not-showing-after-clear-installation/500