Code Monkey home page Code Monkey logo

strapi's Introduction

strapi

백엔드 없이 백엔드 지식 없이 API를 만들어 보자!

코드를 한줄도 안쓰고 RestApi를 만들 수 있는 라이브러리 📚📚

strapi 사이트

yarn create strapi-app my-project --quickstart
yarn develop

으로 서버를 구동시킬수있습니다~

기본 화면 구성과 항목 추가하기

image

strapi 다운로드 후 로그인 브라우저 창이 뜰 것이다 이것이 바로 대시보드! 우선 대시보드에서 Create New collection Type을 이용해 크게 필드를 생성해줍니다.

image

collection Type을 생성했다면 네비에서 타입들을 확인 할수있고 각 필드의 항목을 추가할 수도 있습니다! ✨✨

image

테스터용 유저도 하나 생성해 줍니다~

image

이후 포스트맨으로 테스트 해봅니다~ url를 잘 적어두고 Send를 보내면 땋 잘 나오는걸 확인 할 수 있습니다~~

로그인하기 (JWT토큰이 자동생성?! 👸👸)

Strapi는 기본적으로 로그인까지 전부 세팅이 되어있습니다

image

대시보드에서 미리 등록해둔 user을 통해 로그인하게되면 jwt토큰이 자동 생성되어있는것을 확인 하실 수 있습니다.!

이외에 커스터 마이징을 위해선 Strapi 문서를 참고하여 수정 가능합니다.

strapi 커스터마이징 하기

  1. post 작성시 auth 유저정보 저장시키기 👩‍💻👩‍💻 코드보기
'use strict';

const { parseMultipartData, sanitizeEntity } = require('strapi-utils');

module.exports = {
  async create(ctx) {
    let entity;

    let user = ctx.state.user.id;
    //유저의 고유번호 id를 가져와
    ctx.request.body.author = user;
    // body 의 author 자리에 넣어줍니다.
    
    // strapi 문서 참고 코드 👇👇
    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);
      entity = await strapi.services.post.create(data, { files });
    } else {
      entity = await strapi.services.post.create(ctx.request.body);
    }
    
    return sanitizeEntity(entity, { model: strapi.models.post });
  },
};
  1. post의 view수를 카운팅 하도록 커스터마이징 👩‍💻👩‍💻 코드보기
'use strict';

const { default: createStrapi } = require("strapi");

/**
 * Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
 * to customize this model
 */

module.exports = {
    lifecycles:{
        async afterFindOne(result, params, populate){
        //strapi의 쿼리 post를 찾고, 쿼리를 불러올때마다 view를 1씩 증가시키도록 
            const res = await strapi.query('post')
            .model.query(q => {
                q.where('id', result.id);
                q.increment('view',1);
            }).fetch();
            console.log(res)
        }
    }
};

strapi's People

Contributors

sunhwa508 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.