This is the 8th day of my participation in Gwen Challenge

A little random thought, don’t spit fast ~

Today, I read zhang Xinxu senior’s “Learn not to go in, no time to learn how to do?” , deeply touched, briefly.

I think this article is worth me to read again and again. At present, I have only read it briefly for two times. It is very touching to say briefly. The article says, “The whole thing of learning professional skills, alas… Like hard scalp to drink a bowl of boiled water, look back, hemp duck, there is a cylinder of boiled water to drink, and is sima light hit the cylinder to save so big a cylinder. This plain boiled water is tasteless, the amount is large, and drank a few bowls of what effect also can’t see, think about it, or to watch plays and play games interesting, the final performance is to want to learn, unable to learn. “

How to say, my idea is: if we are in the comfort zone, we do drink juice, drink milk, drink a little wine, drink many, many, but, if we in sorching summer, we are more than 30 degrees of high temperature hot environment thirsty for a long time, if at this time have a cup of plain boiled water placed in front of you, you will feel that plain water is the most delicious thing in the world drink? Maybe a change of scenery, maybe you come home and you have a drink, you have beer, but, at least then, plain water is what we need most, what tastes best. To put it another way, drinking plain water made us last long enough to get a drink. So said so much, just want to say: learning like drinking water, insipid, but can solve the dilemma sorrow. Drink it often, and pour out sorrow.

To the body

Following the previous basic article “Node Basics”, the basic environment has been set up.

The routing process

Create a level 1 directory SRC and create blog.js and user.js under SRC

blog.js

const { getList } = require('.. /controller/blog')
const { SuccessModel, ErrorModel } = require('.. /model/resModel')
const handleBlogRouter = (req, res) = > {
  const method = req.method
  // Get the blog list
  if (method == 'GET' && req.path == '/api/blog/list') {
    const author = req.query.author || ' '
    const keyword = req.query.keyword || ' '
    const listData = getList(author, keyword)
    return new SuccessModel(listData)
  }
  // Get blog details
  if (method == 'GET' && req.path == '/api/blog/detail') {
    return {
      msg: 'This is the interface to get blog details'}}// Create a new blog
  if (method == 'POST' && req.path == '/api/blog/new') {
    return {
      msg: 'This is the interface to create a new blog'}}// Update a blog post
  if (method == 'POST' && req.path == '/api/blog/update') {
    return {
      msg: 'This is the interface to update your blog.'}}// Delete a blog post
  if (method == 'POST' && req.path == '/api/blog/del') {
    return {
      msg: 'This is the interface for deleting blogs'}}}module.exports = handleBlogRouter

Copy the code

user.js

const handleUserRouter = (req, res) = > {
  const method = req.method

  / / login
  if (method == 'POST' && req.path == '/api/user/login') {
    return {
      msg: 'This is the login interface.'}}}module.exports = handleUserRouter
Copy the code

Return model processing

SRC create a new model folder and create resModel.js

resModel.js

class BaseModel {
  constructor(data,message) {
    if(typeof data == 'string') {
      this.message = data
      data = null
      message = null
    }
    if(data) {
      this.data = data
    }
    if(message) {
      this.message = message
    }
  }
}

class SuccessModel extends BaseModel {
  constructor(data,mesage) {
    super(data, message)
    this.errno = 0}}class ErrorModel extends BaseModel {
  constructor(data,message) {
    super(data,message)
    this.errno = -1}}module.exports = {
  SuccessModel,
  ErrorModel
}
Copy the code

Controller to return data

Create a new controller folder, the same as model, and create blog.js under Controller

controller/blog.js

const getList = (author, keyword) = > {
  // Return mock data
  return[{id: 1.title: 'title XXX'.content: Content of the 'A'.createTime: 1623165670787.author: 'Tmier1'
    },
    {
      id: 2.title: 'title name' LLL '.content: Content of the 'B'.createTime: 1623165731256.author: 'Tmier2'}}]module.exports = {
  getList
}
Copy the code

Then update app.js

app.js

const querystring = require('querystring')
const handleBlogRouter = require('./src/router/blog.js')
const handleUserRouter = require('./src/router/user.js')
const serverHandle = (req, res) = > {
  res.setHeader('Content-Type'.'application/json')

  / / get the path
  const url = req.url
  req.path = url.split('? ') [0]

  / / query
  req.query = querystring.parse(url.split('? ') [1])

  // Handle blog routing
  const blogData = handleBlogRouter(req, res)
  if (blogData) {
    res.end(JSON.stringify(blogData))
    return
  }
  // Process the user route
  const userData = handleUserRouter(req, res)
  if (userData) {
    res.end(JSON.stringify(userData))
    return
  }
  // No route is matched, 404 is returned
  res.writeHead(404, {"Content-Type": "text/plain"})
  res.write("404 Not Found\n")
  res.end()
}
module.exports = serverHandle
Copy the code

OK, that’s all for today, and we’ll continue to learn Node~ tomorrow