xinpianchang / fe-weekly

weekly for fe-team
10 stars 2 forks source link

Strapi #43

Open dailynodejs opened 3 years ago

dailynodejs commented 3 years ago

Strapi 是什么?

the open-source Headless CMS (opens new window)developers love.

创建 Content-Type

这个是创建表时默认结尾会加个s,在高级设置中可以自己设置表名

image

访问方式

Method Path Description
GET /{content-type} Get a list of {content-type} entries
GET /{content-type}/:id Get a specific {content-type} entry
GET /{content-type}/count Count {content-type} entries
POST /{content-type} Create a {content-type} entry
DELETE /{content-type}/:id Delete a {content-type} entry
PUT /{content-type}/:id Update a {content-type} entry

Single Types

api 文件夹

config 文件夹

config/functions/bootstrap.js

官网地址

The bootstrap function is called at every server start. You can use it to add a specific logic at this moment of your server's lifecycle.

我们在这里打印一下 strapi:

{
  reload: {}
  app: {},
  router: {},
  server: {},
  log: {},
  utils: {},
  dir: '',
  admin: {},
  plugins: {},
  config: {},
  isLoaded: false,
  fs: {}
  eventHub: {},
  api: {},
  components: {},
  middleware: {},
  hook: {},
  connections: {},
  contentTypes: {},
  models: {},
  controllers: {},
  services: {},
  webhookRunner: {},
  db: {},
  store: [Function],
  webhookStore: {},
  entityValidator: {},
  entityService: {},
  telemetry: { send: [AsyncFunction: send] },
  errors: [Function: Error] 
}

打印一下:strapi.app.use

use(fn) {
    if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
    if (isGeneratorFunction(fn)) {
      deprecate('Support for generators will be removed in v3. ' +
                'See the documentation for examples of how to convert old middleware ' +
                'https://github.com/koajs/koa/blob/master/docs/migration.md');
      fn = convert(fn);
    }
    debug('use %s', fn._name || fn.name || '-');
    this.middleware.push(fn);
    return this;
  }

自定义404

config/functions/responses/404.js

'use strict';

module.exports = async ( ctx ) => {
  return ctx.notFound('My custom message 404');
};

访问一个不存在的地址:

{"statusCode":404,"error":"Not Found","message":"My custom message 404"}

命令

strapi develop

strapi build

strapi start

plugin

strapi-plugin-content-manager

dailynodejs commented 3 years ago

strapi/lib/middlewares/router/index.js

用到了:

const Router = require('koa-router');

有 2 个路由配置:

strapi.config.routes

_.forEach(strapi.config.routes, value => {
});

strapi.admin.config.routes

_.get(strapi.admin, 'config.routes', []).forEach(route => {

 });
dailynodejs commented 3 years ago

admin

配置:config/server.js

admin Admin panel configuration Object  
admin.auth Authentication configuration Object  
admin.auth.secret Secret used to encode JWT tokens string undefined
admin.url Url of your admin panel. Default value: /admin. Note: If the url is relative, it will be concatenated with url. string /admin
admin.autoOpen Enable or disabled administration opening on start. boolean true
admin.watchIgnoreFiles Add custom files that should not be watched during development. See more here (opens new window)(property ignored). Array(string) []
admin.host Use a different host for the admin panel. Only used along with strapi develop --watch-admin string localhost
admin.port Use a different port for the admin panel. Only used along with strapi develop --watch-admin string 8000
admin.serveAdminPanel If false, the admin panel won't be served. Note: the index.html will still be served, see defaultIndex option boolean true
admin.forgotPassword Settings to customize the forgot password email (see more here: Forgot Password Email) Object {}
admin.forgotPassword.emailTemplate Email template as defined in email plugin Object Default template(opens new window)
admin.forgotPassword.from Sender mail address string Default value defined in your provider configuration
admin.forgotPassword.replyTo Default address or addresses the receiver is asked to reply to string Default value defined in your provider configuration