mikebell.xyz


If you need a Computer Science tutor, code reviewer, or someone to pair program with, click here



Finding Bugs in Languages/Frameworks You Don’t Know: node.js

description: node.js isn’t so hard after all! :D tags: javascript, node.js, webdev, json


I am helping my first node.js student. He’s writing a server in node.js and I helped him find a missing parenthesis, curly-bracket, and semi-colon.

I don’t know node.js, but I know enough Javascript to help.

Am I qualified to find problems in node.js projects now?

Can I put this on my resume?

How is this possible?


Questions that I asked myself during the session in order to help them:

  1. How to check if an object is an array?
  2. How to check if an object is a dictionary?
  3. How to verify if a function callback occurs?
  4. How do I access values in a dictionary?
  5. Do semi-colons matter in Javascript?
  6. How do I handle the POST request body in Node.js?

1. How to check if an object is an array?

This one is easy. There is a built-in method for this:

isArray = Array.isArray(someArray);

2. How to check if an object is a dictionary?

This one isn’t so straight-forward, but if you know the keys that should be on it, you can try to access them.

v = someDict["someKnownKey"];

If a value exists for a known key, then the object is a dictionary.

I should go into further detail on this one…it is not sufficient or always possible to know the keys ahead of time.


3. How to verify if a function callback occurs?

I like to use alert() if possible, but otherwise console.log() will suffice.


4. How do I access values in a dictionary?

v = someDict["someKey"];

5. Do semi-colons matter in Javascript?

Originally, I thought “nah” but…

https://stackoverflow.com/questions/11978698/do-we-need-a-semicolon-after-function-declaration?lq=1

Turns out they do!

Just like in C, a function definition does not need them:

function test() {
    // ...
}

But, if you’re assignment a function to a variable:

var f = function test() { 
    // ...
};

THEN, it matters.


6. How do I handle the POST request body in Node.js?

https://itnext.io/how-to-handle-the-post-request-body-in-node-js-without-using-a-framework-cd2038b93190

At the top of your file, add this line:

const { parse } = require("querystring");

Then, you can parse a POST body like this:

parsedData = parse(postData);

The returned object is a JSON dictionary, so you can access values on it in the same way.


I managed to help my student work through every single bug we encountered in 90 minutes, and managed to solve all of the problems that they had.

It is funny how I have virtually zero experience working with node.js, and yet I was able to dig through the problems that they were having, identify syntax errors using simple indentation procedures to line-up code blocks, ask some basic questions about the intended operation of the program, verify assumptions about the state of objects/variables, and move things from previously-broken to currently-working.

This is a thought-process that exists throughout the tech world, and is the driving factor by which progress is made and how things are discovered and built. Just ask questions. Once you have worked with so many languages and frameworks you get a general feel for all of them, which broadens your ability to identify problems in both small and large contexts.


If you need a Computer Science tutor, code reviewer, or just someone to pair program with, hit me up