1. Implement the image uploading interface

Requirements: Zhihu users edit information picture upload

Analysis: Upload the image to the server and return the URL

  1. Because you want to recognize parameters of type file, you need to use koa-body, which koA-BodyParser cannot recognize.
const KoaBody = require('koa-body')
app.use(KoaBody({
  multipart: true.formidable: {
    // Set the upload address
    uploadDir: path.join(__dirname, '/public/uploads'),
    // Keep the image suffix
    keepExtensions: true}}))Copy the code
  1. To return the URL, you need to use KOA-static to manage static resources
const KoaStatic = require('koa-static')
app.use(KoaStatic(
  path.join(__dirname, staticPath)
))
Copy the code
  1. Register to upload picture routing
router.post('/upload', upload)
Copy the code
  1. Implement the upload controller function
// home.js
upload (ctx) {
  const file = ctx.request.files.file
  const basename = path.basename(file.path)
  ctx.body = {
    url: `${ctx.origin}/uploads/${basename}`}}Copy the code
  1. Use Postman tests

2. Implement user data editing interface

Requirements: as shown in the figure, the interface for editing user information can be realized

  1. Redesign the user’s Schema to add more fields than before
const UserSchema = new Schema({
  ...
  avatar_url: {type: String},
  gender: {type: String.enum: ['male'.'female'].default: 'male'},
  headline: {type: String},
  locations: {type: [{type: String}].select: false},
  business: {type: String.select: false},
  employments: {
    type: [{
      company: {type: String},
      job: {type: String}}],select: false
  },
  educations: {
    type: [{
      school: {type: String},
      major: {type: String},
      diploma: {type: Number.enum: [1.2.3.4.5]},
      enterance_year: {type: Number},
      graduation_year: {type: Number}}].select: false
  },
  following: {
    type: [{type: Schema.Types.ObjectId, ref: 'User'}].select: false}})Copy the code
  1. Modify the Update method in the Users controller
async update(ctx) {
  ctx.verifyParams({
    name: {type: 'string'.required: false},
    password: {type: 'string'.required: false},
    avatar_url: {type: 'string'.required: false},
    gender: {type: 'string'.required: false},
    headline: {type: 'string'.required: false},
    locations: {type: 'array'.itemType: 'string'.required: false},
    business: {type: 'string'.required: false},
    employments: {type: 'array'.itemType: 'object'.required: false},
    educations: {type: 'array'.itemType: 'object'.required: false}})const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body)
  if(! user) {ctx.throw(404.'User does not exist')}
  ctx.body = user
}
Copy the code
  1. Modify the findById method in the Users controller to implement the filtering field
async findById(ctx) {
  const {fields} = ctx.query
  const selectFields = fields.split('; ').filter(f= > f).map(f= > '+' + f).join(' ')
  const user = await User.findById(ctx.params.id).select(selectFields)
  if(! user) {ctx.throw(404.'User does not exist')}
  ctx.body = user
}
Copy the code
  1. Use Postman tests

Conclusion:

  1. The general steps for writing an interface are:
  • A. Define the data model Schema

  • B. Write a forwarding route

  • C. Write controller logic using data model

  • E. Use the Postman test

  • F. Write unit tests and pressure tests

2. Updating or deleting user information requires authentication.