This is the third day of my participation in Gwen Challenge

This article is the NodeJs Restful API development blog.csdn.net/geeklevin/a… Further supplement of.

directory

An overview of the

Starting with simple

Look at the renderings first

So let’s go back to the principle

Here’s a quick look at the curl tool

This paper focus on


An overview of the

CRUD is what we call Create Create, Read, Update Update, Delete Delete.

These operations are some of the most common in back-end development, for example, a web site that sells merchandise. It involves the CRUD operation of adding and putting commodities on the shelves, providing commodities for users to browse, as well as the subsequent updating of commodity attributes, removal from the shelves to the final removal, all centering on a commodity. Of course, the actual operation is more complicated.

 

Starting with simple

The previous article wrote a more parameter name printed to the response returned to the client, displayed in the browser.

In this article we will create a product, and then we will make the following four interfaces around the product:

  1. Adding product Information
  2. Reading commodity information
  3. Update commodity information
  4. Delete the goods

Look at the renderings first

  • Read all the goods, open a browser to visit: http://localhost:8080/products

FireFox right-click -> View Elements, select FireFox because the results are automatically formatted. For Chrome you can right-click -> Check)

In the web column above, we can go in and see that the browser accessing this interface link is essentially making a GET request.

Curl curl curl curl curl curl curl curl curl curl

Curl -x GET target link

Curl curl curl curl curl curl curl curl curl

In the figure above, the interface test window is on the left and the Web process serving the interface is on the right.

The curl tool is used to add, delete, and update the web API

  • Adding product Information

curl -X POST -H “Content-type: application/json” -d ‘{“id”:5,”name”:”Cannon”,”type”:”Camera”}’ http://localhost:8080/produc

 

  • Delete the product information, the effect is as follows

Before deleting the product, query the information about the product whose ID is 4 and then delete it. The result is as follows

The web process on the right outputs the latest product information. There is no record with ID =4, and the information is deleted successfully.

 

  • Query product information. The result is as follows

Select * from product where id=4

curl -X GET http://localhost:8080/product/4

Select * from product where id=5 and id=5;

curl -X GET http://localhost:8080/product/5

  • Modifying commodity information

# We plan to change the name of the product with id=5 to CannonX2

curl -X PUT -H “Content-type: application/json” -d ‘{“id”:5,”name”:”CannonX2″,”type”:”Camera”}’ http://localhost:8080/product

 

So let’s go back to the principle

 

Here’s a quick look at the curl tool

Curl is a common Http request client for Linux operating systems. You can run the yum install -y curl command to quickly install curl.

Curl curl curl curl curl curl curl curl curl

Curl. Se /download. Ht…

 

This paper focus on

The following code focuses on the analysis of the above code implementation of multiple interfaces.

About the server (HTTP service creation) has no comments, again can review article: blog.csdn.net/geeklevin/a… .

const restify = require('restify');

// The js module loaded with product information is assigned to the PM constant
const pm = require('./product.js');
// Go back to the first illustration above and print all product information before accessing the link
pm.show();
console.log(pm.query(1));

function read(req, res, next) {...// Omit the code snippet to get the id in the request
   // Call the query product method of the PM object
   res.send(pm.query(id))
}

function dele(req, res, next) {...// Omit the code snippet to get the id in the request
   // Call the delete product method of the PM object
  res.send({status:'deleted'.msg: pm.delete(id)})
}
function readAll(req, res, next){
   // Call the read all product method of the PM object
   res.send(pm.getData())
}
function create(req, res, next){...// omit the product snippet from the request
   // Call the add product method of the PM object
   pm.add(product);
}
function update(req, res, next){...// omit the product snippet from the request
   // Call the modify product method of the PM object
   pm.update(product) 
}

// This was mentioned in the previous article
var server = restify.createServer();

server.use(restify.plugins.bodyParser({ mapParams: true }));

// Bind the readAll method to the /products GET action
server.get('/products',readAll);

// Bind search/add/change/DELETE to GET/POST/PUT/DELETE respectively.
server.get('/product/:id', read);
server.post('/product', create);
server.put('/product', update);
server.del('/product/:id', dele);


// The server listens on port 8080
server.listen(8080.function() {
  console.log('%s listening at %s', server.name, server.url);
});
Copy the code

There are four common HTTP request methods that are bound above via restify’s corresponding Server object. This enables different responses to different request methods for the same link.

Here, we take another update product action parsing:

curl -X PUT -H “Content-type: application/json” -d ‘{“id”:5,”name”:”CannonX2″,”type”:”Camera”}’ http://localhost:8080/product

Using curl to send a PUT request to the /product interface

Server: Finds the /product interface valid and binds the update product method to the PUT method to perform the update product method.

I recently thought of a RESTFly tool I was working on and put off writing this article for a long time. Readers are welcome to comment on the principle of other additions, deletions and queries.

 

PS: Above is the key code parsing, leaving out a lot of code (not as executable final version)

Complete project code:

Codechina.csdn.net/geeklevin/n…