This week we focused on creating RESTful API's. We used a blog tutorial written by Jeff Anderson. In Building a Node.js REST API with Express, Jeff goes through step by step what needs to happen in order to set up, respond and test an API that you develop.

I found this tutorial really helpful as I have been struggling with postgres and http requests over the last month or so, and Jeff does a great job at explaining alot of my questions throughout the tutorial. It also helped that he was on hand to ask questions to throughout each step of the process, and I had a lot of questions.

Through this tutorial I learned that in a REST API each HTTP Request responds to an action against a resourve serviced by teh API. This means that HTTP get function retrives a resourves object, and the post function creates a new resource object etc.

So when a user compiles some data, attaches it and sends it to our server, they are using the HTTP post request to create a new resource object. Here we need to use our route handles to read and use this data. We want to use the bodyParser middleware to handle and parse the newly created data. We want to make it so the bodyParse reads only JSON data and parses it for us.

We want to make sure that this information is valid to make sure that we aren't accepting files super large for our server. Validation happens with Express through our routes URI and through middleware.

Once we've created data we want to be able to display it easily and let the user choose what they want to see. This is where pagination comes in. Pagination allows us to display loads of data in a simple structure. We can choose how many items are to be displayed per page. (think kijiji).

We want to use multer, a middleware that is specific for photos to control the size of the file. Also since our user could be silly and name the file any number of things we want to make sure that we standardize it a bit. Therefore we use this magic code:

filename=filename.replace(/\W+/g, '-').toLowerCase();

therefore we will only get normalized filename. We will also add a Date.now() stamp to it so if multiple people want to upload a file called cat.jpg, as much as I hate them, I will allow it. With the datestamp it will not override or inform the user that this name is already in use.

Getting the photos uploadable was really exciting and I wanted to work on my app, ePic with it. Because I have been tinkering with passport and postgres over the last couple of weeks, I decided to revisit this. The way Jeff set up his code for photos made it really easy for me to see where I was making object errors in my users.js file for passport. So I went back and refactored/edited my users.js and linked up postgress, passport and this tutorial.

I had a really hard time with the integration testing and this is something I definitely want to work on. As far as I could tell this was way more work than just doing simple tests/refreshes. But that's probably becuase it was my first try at setting it up and in the long run will save me time! So the Integration testing is something I definitely want to get back to!

I am currently working on refactoring my code (i have photo.js but 1/2 that code is in index.js right now). I've linked up sessions to photos (i think) but i need to test this and I want to be able to print stuff to the profile page. I also have not done any of this in angular or ionic so i will work on integrating that now.

comments powered by Disqus