Express.Js – Survey Form


Turns out that creating on server on Express.js is significantly easier than in Node.js!

I went through the practice material and starting the first assignment to create a survey form which I figured out relatively easy, which surprised me a little.

The assignment was to have the user fill out a survey form below:

Screen Shot 2016-05-08 at 7.51.09 PM

This information should then be saved in, using a callback function to save the data into an array that will later be used to display the results on a different page called /result.

Having the data appear on a separate page was fairly simple and the syntax to have the data display the results was similar to how we did in PHP which was to access the key (in this case: users) and the name of the field to be displayed such as name, location etc.

Screen Shot 2016-05-08 at 7.55.40 PM (2)

We were to have a button ‘Go Back’ to go back to the main page where the form was originally displayed. Initially, I had a input type submit with a value of “Go Back” wrapped into a form which would send a get request to get the page of index (the original page). I was having trouble with this though because the url had a ? mark next to the localhost like this: localhost:8000/? which I found out is because it’s expecting data from get but since it receives nothing, it returns a question mark. The instructor told me a better way which is to create a button and wrap it in a link with a href pointing to the home page. Now, it works like a charm.

My server.js file below:

Screen Shot 2016-05-08 at 7.52.58 PM (2)


Starting NODE.js:

Nodemon didn’t like this:

Screen Shot 2016-05-08 at 7.42.50 PM

I had my curly brace and parenthesis in the wrong order, should be }); works like a charm now.

I like that it shows you where you made a mistake in your code and what line in what file.

I had a pretty lousy day at the algorithm board this morning: this morning’s challenges were to make a copy of a stack using only a queue for storage and to create a queue function using two stacks. I thought I had a great idea for the copy stack, I looked at it the night before until I asked the opinion of the instructor who led me to realize that I didn’t know if the stack given was an array or a linked list. My group of 2 other cohort mates were stumped trying to figure this out but we didn’t give up trying, we kept writing ideas for algorithms on the board. Afterwards, a couple of our fellow cohort mates came up to us and started walking us though what we had and explained why it wouldn’t work and what we should have done. I went through, writing on the board, interpreting what they were saying… I have to say I appreciate the feedback from them. We didn’t finish the algorithms and when another group presented the copy stack function it seemed we were heading in the right direction, we just didn’t get the answer. Nonetheless, I understood their solution. The same goes for the other algorithm, I just didn’t get enough time to really think about it considering we spent our time on the first one. Stacks and Queues together are quite tricky. I need to spend more time on them this weekend going over the basics again and using that to build some of the functions that are presented as challenges in the book. (The book being the Algorithm Challenges book created by Martin, an instructor for the Dojo and he went through the program when the founder was teaching the program).

I ran into some little problems, like the one above as well as when I worked on an assignment where we condense the code we need to run the .html, .css, images and .js files…I merely didn’t realize, my server was trying to open a directory named images/images/_img.jpg because I specified it to look in the images folder and on my html I put the image src as /images/_img.jpg…I scratched my head looking at this one for about 30 mins before asking the instructor to take a look at it…He explained to me the problem I had above and I fixed the code.

Man, it’s always a little thing that’s the problem.

It was a pretty light day, I finished the whole Node.js section, including all the assignments and was feeling like a boss but that thought quickly faded because tomorrow we start Express.js which is supposed to make the server a lot more easier for us, this is also when we start to put into practice MVC which from doing LAMP last month seems like an easy concept to grasp just finding the right syntax in javascript to execute it properly.

I skipped out early today as did a couple other of my cohort mates after I tried to help a fellow co-horter with an assignment. It turns out he isn’t running Nodemon(which automatically updates your server for you without you having to restart it) on his machine because he was unable to install in properly. I was unaware of this so I was thinking the problem was in his server.js file which technically it was, the server hadn’t been restarted…Another cohort mate reached that conclusion after tinkering with it for a few minutes and I left out shortly thereafter.