Creating a Custom API with Strapi - A Node Headless CMS

栏目: IT技术 · 发布时间: 4年前

内容简介:Strapi is an amazing tool that lets us build our backends without writing a line of code. It comes with an API out of the box.While other Headless CMSs can be focused more on helping creating blogs (and that's perfectly fine), Strapi can be used to build m

Strapi is an amazing tool that lets us build our backends without writing a line of code. It comes with an API out of the box.

Table of Contents

  • Getting Started with Strapi
  • Creating Our Content Type
  • Strapi Dashboard Updates Your Code
I would use Strapi as a Headless CMS because it allows for flexibility in content types. We could build a custom app whereas other CMSs are geared towards blogs.

While other Headless CMSs can be focused more on helping creating blogs (and that's perfectly fine), Strapi can be used to build more of an app than a blog. The ability to create all types of content is what gives Strapi this flexibility. We can also jump into the code of our Strapi installation and do more custom things like customizing our dashboard , making database queries , formatting content after an update , and programmatically sending emails .

The thing I love about Strapi is how easy it is to create your own custom APIs from whatever content you may have. Let's say we wanted to have a type of content called Pokemon . We could quickly build out our content fields and structure.

Creating a Custom API with Strapi - A Node Headless CMS

Let's dig into using Strapi.

There are a couple ways we can get started with Strapi .

  1. Create a Strapi installation locally. Push to a server later
  2. Create a Strapi installation on a server

If you wanted to work locally first, then you can create a Strapi application from the command line:

New Course: Make 20 React Apps
# with yarn
yarn create strapi-app my-strapi-project --quickstart

# or with npx
npx create-strapi-app my-strapi-project --quickstart

Using the --quickstart flag will make Strapi use SQLite for its database. If you don't use --quickstart , you should install a local database like MongoDB .

We'll be starting with a base Strapi installation on aDigitalOcean server. We have anarticle on how to quickly Install Strapi with 1-Click on DigitalOcean . We also have a video of installing Strapi on DigitalOcean.

Getting Started with Strapi

The Strapi Quick Start Guide and Tutorial are good places to start with Strapi. Let's walk through building something with Strapi in this tutorial.

Once you have Strapi installed either locally or on a server, you'll be able to see your new installation. At http://localhost:1337/ if working locally and at your new server's IP if working on a server.

We'll have to create a new admin account:

Creating a Custom API with Strapi - A Node Headless CMS

Once you create your new admin account, you'll see your new dashboard.

Creating a Custom API with Strapi - A Node Headless CMS

Notice that there are some Get Started Videos in the bottom right to give you some visual tutorials.

Let's look at what we'll build to familiarize ourselves with the Strapi dashboard.

Lets build a small API that we will be able to use in our frontend applications. The overall flow of using Strapi would look like:

  1. Create content types
  2. Create content
  3. Use Strapi's API to grab content (use a frontend tool like Gatsby or Next.js )
Let's build a tiger listing app!

Seeing as how that Tiger King Netflix show is so popular, lets build a tiger tracking app.

Creating Our Content Type

We have to build our content type for a tiger. Let's add the following properties with these specific types:

  • name: Text
  • age: Number
  • weight: Number (in lbs)
  • length: Number (in inches)
  • location: Select

Let's start by creating the content type. Your dashboard will have a Create a Content-Type button when you start.

Creating a Custom API with Strapi - A Node Headless CMS

Once we've created a Tiger content type, we'll be able to add our properties via the dashboard. This name should always be singular.

Creating a Custom API with Strapi - A Node Headless CMS

Let's add a Text field and call it Name .

Creating a Custom API with Strapi - A Node Headless CMS

We can even click into advanced settings and make this a Required field .

Creating a Custom API with Strapi - A Node Headless CMS

We've been able to create a new content type, add it to our database, and generate relationships without ever writing a single line of code! We are able to use our coding skills to focus on the frontend code instead of worrying about the backend code.

Strapi Dashboard Updates Your Code

One of the cool things about Strapi is that you can use either this GUI dashboard or work within code to create your models and schema. If you are working locally, you can open up your files and see how this dashboard change has updated your code. You could keep your code in a GitHub repo, work locally, deploy to your DigitalOcean server, and that would be a great setup.

You'll find the changed files under /api . There is a new tiger folder for this content type we've created.

Creating a Custom API with Strapi - A Node Headless CMS

Open up tiger.settings.json and you can see our new Name attribute.

Creating a Custom API with Strapi - A Node Headless CMS

This connection also goes from code to dashboard. Let's add a new field from within our code:

"attributes": {
  "Name": {
    "type": "string",
    "required": true
  },
  "Age": {
    "type": "integer"
  }
}

Save this file and go to your dashboard and you'll see your new field called Age .

Creating a Custom API with Strapi - A Node Headless CMS

This is a cool feature of Strapi, that everything in the dashboard corresponds to a line in your code. For the most part, we can do all of our work from within the dashboard. You may want to jump into the code if you want to customize Strapi further.

Let's add the rest of our fields from our dashboard.

One of the interesting fields to add is Location . We can use an Enumeration field to create a dropdown of options.

Creating a Custom API with Strapi - A Node Headless CMS

Once we have all our fields, our dashboard will show this:

Creating a Custom API with Strapi - A Node Headless CMS

Feel free to add whatever fields you like. You can even create Components for a repeatable field.

Now that we have our content type created (make sure you hit Save ), let's create some content.

Click on Tigers in the left of our dashboard and click Add New Tiger .

Creating a Custom API with Strapi - A Node Headless CMS

Create a tiger! Creating a Custom API with Strapi - A Node Headless CMS

Strapi even lets us configure how our dashboard looks. Try clicking Configure the view .

Now that we've created some content, let's see how we could use it via an API. Strapi comes out of the box with a REST API .

We need to set our Tiger resource to have public permissions so that everyone can view these tigers. We won't provide public access to create Tigers though.

Click on Roles & Permissions in the left nav. Click on Public . Now we can check the following under Tiger:

  • find
  • findone

Creating a Custom API with Strapi - A Node Headless CMS

Hit save and we can view our API!

// if working locally
http://localhost:1337/tigers

// if on a server 
http://<your-ip-address>/tigers

Creating a Custom API with Strapi - A Node Headless CMS

Create more tigers and see them in your API! You can also grab a single Tiger by adding an ID to the end of your URL: http://localhost:1337/tigers/1

We have the ability to use a REST API out of the box. What if we wanted to add a GraphQL API? Strapi provides this for us as well as a separate plugin. We have to add the GraphQL plugin.

If we click on Marketplace on the left side of our admin dashboard, we are able to see plugins we can add to our Strapi dashboard.

Creating a Custom API with Strapi - A Node Headless CMS

We can view our GraphQL endpoint at:

// if working locally
http://localhost:1337/graphql

// if on a server 
http://<your-ip-address>/graphql

Creating a Custom API with Strapi - A Node Headless CMS

Strapi comes with what they call a Shadow CRUD . It gives us type definition, queries, mutations, and resolvers for GraphQL based on our models (content-types).

If our model looks like this:

{
  "connection": "default",
  "options": {
    "timestamps": true
  },
  "attributes": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "text"
    },
    "open": {
      "type": "boolean"
    }
  }
}

Our generated Shadow CRUD will look like this:

// Restaurant's Type definition
type Restaurant {
  _id: String
  created_at: String
  updated_at: String
  name: String
  description: String
  open: Boolean
}

// Queries to retrieve one or multiple restaurants.
type Query {
  restaurants(sort: String, limit: Int, start: Int, where: JSON): [Restaurant]
  restaurant(id: String!): Restaurant
}

// Mutations to create, update or delete a restaurant.
type Mutation {
  createRestaurant(input: createRestaurantInput): createRestaurantPayload!
  updateRestaurant(input: updateRestaurantInput): updateRestaurantPayload!
  deleteRestaurant(input: deleteRestaurantInput): deleteRestaurantPayload!
}

Strapi docs are good at providing more code for every scenario. Here are some popular ones you may be interested in:

If you found Strapi helpful, check out our installation tutorial and let me know if you want to see more Strapi content.

Like this article? Follow @chrisoncode on Twitter


以上所述就是小编给大家介绍的《Creating a Custom API with Strapi - A Node Headless CMS》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从零开始学C++程序设计

从零开始学C++程序设计

编者:吴惠茹 / 机械工业 / 2017-05-01 / 69.0

一起来看看 《从零开始学C++程序设计》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具