Previous Lecture Complete and continue  

  Param Pam Pam

Welcome back to the expressworks tool. The next venture would be about extracting parameters from the URL. So, parameters in the URL, you would often see this pattern in restful APIs. It's very common pattern when you have an entity or resource. For example, message and then you would have an ID like an object ID. And what we need to do here we need to apply this code. So we need to use crypto and basically create a hash. Sha1 hash from that input. Okay. So let's go to the editor. Create a new file. As always in the beginning I would be importing all the dependencies so we can use them later. And express is the way we create an app. Okay so now app.put message/id. This is how we tell Express.js to look out for this ID. And Express.js from populate reg.params with that ID.

And then I'm just pasting and retyping the code for the hash. Crypto, it's on NPM module. Or actually it's not NPM module. We don't need to install it. So anyways you feel free to look up the crypto module documentation on a nodejs.org. App.listen we're using process.argv2. Okay, so what happening here the put, the HTTP verb, we can have get, post, put, delete and some other methods. So in this case it's a put. It means update this resource. It's a partial update. And then message/:id. So colon tells Express.js that this is a parameter. Express.js automatically parses the value from the URL. We don't need to do anything. We just get this nice object reg.params, with a plural so you can have more than one parameters. For example we can have ... We can have more than one parameters and they would appear in that object as well. And this is just something that crypto uses. So we are creating a string, with a date adding the ID and then hashing it. So let me save it. And let's go to the terminal. Okay. It's running without errors.

Okay, it's passing. So everything works. If you want we can also make this run with a node 1 or node by modifying this line. So we can add 3000. Okay, so our server is running. So we can go to the postman and submit a put request. Okay, so let's remember this 8, 9, 72. Now if we modify ID I would expect it to be different. Okay so now it's different. If you go in a browser you would not see it because the browser is using get not put. That's why we need postman. Let me double check with the expressworks. Okay, it says completed.

0 comments