This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

REST stands for Representational State Transfer, which in Chinese means Representational State Transfer. It first appeared in 2000 in the doctoral thesis of Roy Fielding, one of the main authors of the HTTP specification. In his paper, he said: “The purpose of this article is to understand and evaluate the architectural design of network-based applications in accordance with the architectural principles, and to obtain a functional, high-performance, suitable communication architecture. REST refers to a set of architectural constraints and principles. An architecture is called RESTful if it conforms to the constraints and principles of REST.

REST itself does not create new technologies, components, or services, but the idea behind RESTful is to use the Web’s existing features and capabilities to make better use of some of the guidelines and constraints of existing Web standards.

In conclusion, RestfulApi is a popular style of API design, which can make the interface more clear, concise, more hierarchical, and more maintainable

Let’s simply design an Api for a Restful structure based on Express

Design principles

Before I do that, the principle is that an interface should not contain a verb, but rather a resource, for example, to design a user related interface, including,

  • Get the list of users
  • Get user details
  • Add user
  • Update user Information
  • Deleting user Information

The verbs get, add, up, and del are often added to design interface names

  • Get the list of users /getUserList
  • Get user details /getUserDetail
  • AddUser /addUser
  • Update user information /upDataUser
  • Example Delete user information /delUser

Because a resource identifies an entity, it should be a noun. Uris should not have verbs. The verbs should be placed in the HTTP protocol, such as get, post, put, delete

So the correct name would be

Id indicates the unique ID of the user

function URI Request way
Get the list of users /user get
Get user details /user/:id get
Add user /user post
Update user Information /user put
Deleting user Information /user/:id delete

Is that simple? Just one keyword, user


The instance

Building on the infrastructure of the previous article, Express-MVC created a list of users to get, and then implemented the API for adding users, getting details, updating users, and deleting users in sequence

Get the list of users

The user list interface is not introduced. For more information, see Express-MVC

New users

Add the routing

router.post('/user', user.create)
Copy the code

Add the create method under the user controller

Async function create(req, res, next){let result = await userModel.create (req.body); If (result){res.send({code: 200, data:result, MSG :' successful '})}} if(result){res.send({code: 200, data:result, MSG :' successful '})}}Copy the code

Get user details

Add the routing

router.get('/user/:id', user.show)
Copy the code

Add the show method to the user controller

Async function show(req, res, next){let result = await userModel. findOne({where: {id:req.params.id}}) if(result){res.send({code: 200, data:result, MSG :' query successfully '})}}Copy the code

Delete user

Add the routing

router.delete('/user/:id', user.del)
Copy the code

Add the del method to the user controller

Async function del(req, res, next){let result = await userModel.destroy ({where:{id: req.params.id}}); If (result){res.send({code: 200, data:result, MSG :' delete successfully '})}} if(result){res.send({code: 200, data:result, MSG :' delete successfully '})}}Copy the code

Update user Information

Add the routing

router.put('/user/:id', user.update)
Copy the code

Add the update method to the user controller

Async function update(req, res, next){let result = await userModel. update({age: req.body.age, name:req.body.name },{where:{ id: req.body.id }}); If (result){res.send({code: 200, data:result, MSG :' modified successfully '})}} if(result){res.send({code: 200, data:result, MSG :' modified successfully '})}}Copy the code

That’s all for this post, thank you very much for reading here, if this article is good or a little bit of help to you, please like, follow, share, of course, any questions can be discussed in the comments, I will actively answer, thanks again 😁