Back-end interface specification

Convention interfaces generally include the following data

1. What is the path of the current interface? Such as/auth/register – > POST

2. What is the type of data submitted by the current interface? Such as

  • GET Gets data
  • POST commits or creates
  • PATCH Modifying data, partial modification
  • DELETE DELETE data
  • PUT Changes data and replaces the original data

3. Parameter type/format, such as JSON format or Application/X-wwW-form-Urlencoded data

4. Parameter fields and restrictions

For example, a user may need a username and password, and creating a blog requires a blog title and blog content

Or is there any special restriction on the user name

5. Return a successful data format

6. Return the failed data format

You can test the interface with a postman or curl command during development

Certification related

POST /auth/register
Copy the code

Function: User registration

Submit parameters

  • Parameter Type: Content-type: Application/X-www-form-urlencoded; charset=utf-8
  • Parameter field
    • Username: indicates the username. It contains 1 to 15 characters and can only contain alphanumeric digits and underscores (_)
    • Password: contains any 6 to 16 characters

Return the data

  • failure
    • Return format {“status”: “fail”, “MSG “:” error cause “}
  • successful
    • Returns the format
{
 "status": "ok"."msg": "Registration successful"."data": {
   "id": 1,
   "username": "hunger"."avatar": "http://avatar.com/1.png"."updatedAt": "The 2017-12-27 T07:40:09. 697 z"."createdAt": "The 2017-12-27 T07:40:09. 697 z"}}Copy the code

test

#-d is used to pass data

# For POST and PUT: -x POST, for GET, no -x

curl -d "username=hunger1&password=123456" -X POST "http://localhost:3000/auth/register"
Copy the code
POST /auth/login
Copy the code

Function: User login

Submit parameters

  • Parameter Type: Content-type: Application/X-www-form-urlencoded; charset=utf-8

  • Parameter field

    • Username: indicates the username. It contains 1 to 15 characters and can only contain alphanumeric digits and underscores (_)
    • Password: indicates the password, which contains any 6 to 16 characters
  • failure

    • Returns the format{" status ", "fail", "MSG" : "the user does not exist"} or {" status ":" fail ", "MSG" : "password incorrect"}
  • successful

    • Returns the format
{
 "status":"ok"."msg": "Login successful"."data": {
   "id": 1,
   "username": "hunger"."avatar: "Image url","createdAt":"The 2017-12-27 T07:40:09. 697 z","updatedAt":"The 2017-12-27 T07:40:09. 697 z"}}Copy the code

The test command

#-i displays the response header

The response header contains setCookie information

curl -d "username=hunger1&password=123456""http://localhost:3000/auth/login" -i
Copy the code
GET /auth
Copy the code

Run the following command to check whether a user logs in

Submit parameter: none

Return the data

  • You have logged in
{
  "status": "ok"
  "isLogin": true."data": {
    "id": 1,
    "avatar": "http://avatar.com/1.png"."username": "hunger"."updatedAt": "The 2017-12-27 T07:40:09. 697 z"."createdAt": "The 2017-12-27 T07:40:09. 697 z"}}Copy the code
  • No login
  "status": "ok"
  "isLogin": false
}
Copy the code

The test command

# Use the login interface to get cookies, and you can test the login with cookies

curl "http://localhost:3000/auth" -b "connect.sid=s%3AmeDbrn03UtTM8fqChaPQ20wmWlnKeHiu.e3uMtu7j1zQ1iNeaajCmxkYYGQ%2FyHV1ZsozMvZYWC6s"
Copy the code
GET /auth/logout
Copy the code

Run the following command to log out

Submit parameter: none

Return data:

  • failure
    • Return format {“status”: “fail”, “MSG “:” user not logged in “}
  • successful
    • Return the test command in the format {“status”: “OK “,” MSG “: “logout succeeded “}

The test command

curl "http://localhost:3000/auth/logout" -b "connect.sid=s%3AmeDbrn03UtTM8fqChaPQ20wmWlnKeHiu.e3uMtu7j1zQ1iNeaajCmxkYYGQ%2FyHV1ZsozMvZYWC6s"
Copy the code

Blog related

GET /blog
Copy the code

Function: Get a list of blogs

Submit parameters:

  • Page: Page number. The default page is 1. If this parameter is set, the first page of the blog list is obtained
  • UserId: indicates the userId. If no, the system obtains all user data. If this parameter is set, the system obtains the blog list of a certain user
  • AtIndex: specifies whether to display a list of blogs on the home page. If true is passed, only a list of blogs will be displayed on the home page. If false is passed, only a list of blogs will be displayed on the home page

Such as/blog? Page = 2&userid =1 Get the list of blogs on page 2 belonging to user 1

Return data:

  • failure
    • {“status”: “fail”, “MSG “:” system exception “}
  • successful
    • Returns the format
{
  "status": "ok"."msg": "To succeed"."total": 200, // the total number of all blogs"page": 2, // Current page number"totalPage": 10, // Total pages"data": [{"id": 1, // id of the blog"title": "Blog Title"."description": "Brief description of blog content"."user": {
        "id": 100, // user id of the blog,"username": Username of the user who owns the blog."avatar": "Avatar"
      },
      "createdAt": "The 2018-12-27 T08:22:56. 792 z", // Create time"updatedAt": "The 2018-12-27 T08:22:56. 792 z"// Update time},... ] }Copy the code

The test command

curl "http://localhost:3000/blog? page=1&userId=1"
curl "http://localhost:3000/blog? page=1"
curl "http://localhost:3000/blog"
Copy the code
GET /blog/:blogId
Copy the code

Function: Obtain details about a blog whose ID is blogId, such as /blog/1

Submit parameters:

There is no

Return data:

  • failure
    • {“status”: “fail”, “MSG “:” system exception “}
  • successful
    • Returns the format
{
  "status": "ok"."msg": "To succeed"."data": { 
      "id": 1, // id of the blog"title": "Blog Title"."description": "Brief description of blog content"."content": "Blog content, more words."."user": {
        "id": 100, // user id of the blog,"username": Username of the user who owns the blog."avatar": "Avatar"
      },
      "createdAt": "The 2018-12-27 T08:22:56. 792 z", // Create time"updatedAt": "The 2018-12-27 T08:22:56. 792 z"// Update time}}Copy the code
POST /blog
Copy the code

Function: Create a blog

Submit parameters

  • Parameter types:Content-Type: application/x-www-form-urlencoded; charset=utf-8
  • Parameter field
    • Title: blog title. The blog title cannot be empty and contains no more than 100 characters
    • Content: Indicates the blog content. The blog content cannot be empty and contains a maximum of 10,000 characters
    • Description: A brief description of the blog content, which can be empty. If it is empty, the background automatically extracts it from content

Return the data

  • failure
    • Return format {“status”: “fail”, “MSG “:” operation only after login “}
  • successful
    • Returns the format
{
  "status": "ok"."msg": "Create successful"."data": { 
      "id": 1, // id of the blog"title": "Blog Title"."description":  "Brief description of blog content"."contnet": "Blog content"."user": {
        "id": 100, // user id of the blog,"username": Username of the user who owns the blog."avatar": "Avatar url"
      },
      "createdAt": "The 2018-12-27 T08:22:56. 792 z", // Create time"updatedAt": "The 2018-12-27 T08:22:56. 792 z"// Update time}}Copy the code

The test command

curl -d "title=hello&content=world&description=jirengu" -X POST "http://localhost:3000/blog" -b "connect.sid=s%3AdyZh-z5fqPU_ThG9Qn8nGD6euI0UI75e.8uso0k4P6WzqWv02iQCUwxbUML2RdlOCnpKp7RSJpj0"
Copy the code
PATCH /blog/:blogId
Copy the code

Run the following command to modify a blog whose blogId is blogId

Example: / blog / 1

Submit parameters

  • Parameter types:Content-Type: application/x-www-form-urlencoded; charset=utf-8
  • Parameter field
    • Title: The title of the blog, which is optional
    • Content: Blog content, optional
    • Description: A brief description of the blog. This option is optional
    • AtIndex: true/false: Display to the home page or from the home page exception is optional

Return the data

  • failure
    • Returns the format
{"status": "fail"."msg": "Only after login."}
{"status": "fail"."msg": "Blogs don't exist."}
{"status": "fail"."msg": "Can't change someone else's blog."}
Copy the code
  • successful
    • Returns the format
{
  "status": "ok"."msg": "Modification succeeded"."data": { 
      "id": 1, // id of the blog"title": "Blog Title"."description":  "Brief description of blog content"."contnet": "Blog content"."user": {
        "id": 100, // user id of the blog,"username": Username of the user who owns the blog."avatar": "Avatar url"
      },
      "createdAt": "The 2018-12-27 T08:22:56. 792 z", // Create time"updatedAt": "The 2018-12-27 T08:22:56. 792 z"// Update time}}Copy the code

The test command

curl -d "title=hello100&content=world1&description=jirengu2234444444&atIndex=true" -X PATCH "http://localhost:3000/blog/12" -b "connect.sid=s%3At_9V2bMXA7U9oSAmr1dhRXpdRPAsNM2B.jlpWgkwiWdpgTjexeTHGNydt8gvc%2F%2BEkJpQ9yaAmTg0"
Copy the code
DELETE /blog/:blogId
Copy the code

Run the following command to delete a blog whose blogId is blogId

Submit parameter: none

Return the data

  • failure
    • Return format example
{"status": "fail"."msg": "Only after login."}
{"status": "fail"."msg": "Blogs don't exist."}
{"status": "fail"."msg": "Can't delete someone else's blog."}
Copy the code
  • successful
    • Returns the format
{
"status": "ok"."msg": "Deletion successful"
}
Copy the code

The test command

curl -X DELETE "http://localhost:3000/blog/12" -b "connect.sid=s%3AG_Chytg2F0RLWh2wTSCdLWVxpNg1MWWb.nPuMcgyMN6zxuxjSkyu8qSqM1boruol1Nce7egaXrPw"
Copy the code