Keep your mind RESTful (Immersion Day 2)

The year of our lord: 03/01/16

Today instills the second day of our immersion program. The gist of today was implementing Post, Put, and Delete. We began with Post, and got a little help from the wonderful google chrome app, Postman. So let’s get to it.

I’ve made peace with the fact that almost everything I use as a headline is cheesy as hell…although, is hell cheesy? Maybe if you’re an atheist…or lactose intolerant…I’m gonna shut up now..

As I talked about on yesterday’s post (no pun intended), POST is used to create new resources. Here you need to create a new, unique id for each resource. Here is the code I made for post:

//POST takes JSON bodies, so we use jsonParser as middleware//
router.post(‘/product’, jsonParser, function(req, res) {
products.create(req.body);
res.setHeader(‘Content-Type’, ‘application/json’);
var result = {message: ‘in development’};
res.end(JSON.stringify(result));
});

So the main mission here is that we already have a JSON file of “products” that we are using from a previous project. We want to use the post method to add a new id and object into our existing array of objects in the JSON file. This is where Postman comes into play.

what I need to do is use the post setting on Postman, put in my host url so it knows where the new resource information should, and use the header setting to create my header (i.e. ‘Content-Type’: ‘application/json’ that you saw in my code above). In the raw body portion, i put in the new product information wrapped in an object. We then send the object, and it should return with my result message “In development”.

DON’T “PUT” ME, CAUSE I’M CLOSE TO THE EDGE..

Next, we use the put method. As I put in my day one blog, put is basically used to update or alter a new resource. From what I have read, some programmers have used it in the place of post, but, again, not recommended for the quality of practice and for the sake of future programmers who may have to look at your code and decipher whatever cropcircle-esque (patent pending on that word) nightmare you just designed. Put also should return, like other commands, status codes to let the client know whether or not the command has been done properly. Whether it be code 200 (meaning all is well), 204 (meaning there is no content), or the ever popular/disconcerting 404 (content not found). Here is my code for put:

router.put(‘/product/:id’, function(req, res){
products.update(parseInt(req.params.id, 10), req.body);
res.setHeader(‘Content-Type’, ‘application/json’);
var result = {message: ‘in development’};
res.end(JSON.stringify(result));
});

Delete…nothing cute, just delete.

Delete is just that. A method to remove a resource. Here is the code for delete:

router.delete(‘/product/:id’, function (req, res){
products.removeProduct(parseInt(req.params.id, 10));
res.setHeader(‘Content-Type’, ‘application/json’);
var result = {message: ‘It is deleted’};
res.end(JSON.stringify(result));
});
Show your support

Clapping shows how much you appreciated The Fidelity Salesman’s story.