JSON
It’s hard to imagine JavaScript and Node without JSON, which stands for JavaScript Object Notation. In fact, it’s hard to imagine the modern programming universe without it. It’s a simple yet incredibly versatile means for describing and sharing data between applications.
The basics are simple:
{
“artist”: “Rembrandt”,
“work”: “Self Portrait”,
“url”: “https://id.rijksmuseum.nl/200107952”
}
There’s nothing much to see here; just a pair of curly braces and colons with name/value pairs. You can also have arrays and other JSON objects inside of that for complete object graphs. But beyond that is range of techniques and tools for manipulating JSON, starting with the built in JSON.stringify
and JSON.parse
, to turn JSON into strings or vice versa.
JSON is more or less the way to send data over the wire, from client to server and elsewhere. It also is now common in datastores, especially with NoSQL databases like MongoDB. Getting comfortable with JSON will make programming in Node much smoother.
Error handling
Good error handling is a key concept in Node. You’ll encounter two kinds of errors in Node: the standard runtime kind and the asynchronous kind.
Whereas async errors on Promises
are handled by using the .catch()
callback, normal runtime errors are handled with try
/catch
keyword blocks:
try {
/* do something risky */
} catch (error) {
/* do something with the error */
}
If an error occurs during the asynchronous operation, that callback will execute and receive the error object.
When using async
/await
, you use the standard try
/catch
blocks.
Conceptually, the important thing to bear in mind is that things will go wrong, and catching errors is the main way to address it. We want to recover the operation if possible. Otherwise, we want to make the error condition as painless as we can for the end user. We also want to log the error if possible. If we are interacting with an external service, we might be able to return an error code.
The best error handling is dictated by the circumstances. The main thing is to keep errors in the back of your mind. It’s the error conditions we don’t think about that usually get us, more than the ones we think about and get wrong. Also, beware of “swallowing” errors, which is to define a catch
block that does nothing at all.
Keep it flexible
At the beginning of this article, I mentioned that JavaScript can handle a variety of programming styles. These are functional, object-oriented, reactive, imperative, and programmatic. Here we have a vast range of powerful concepts—some of the most important in programming. In the next paragraph, we’ll cover each of them in detail …
Just kidding! There’s no way to do justice to all these programming styles, even in a book-length treatment. The main point is that JavaScript is flexible enough to let you embrace them all. Use what you know and be open to new ideas.
You’ll be often faced with work that must be done, and there is always more than one way to do it. Being concerned that there is a cooler, faster, or more efficient way to get the job done is normal. You have to find a balance between doing the work now and researching newer technologies and best practices for later.
As you add to your understanding, you’ll naturally find tasks and areas where certain paradigms fit. Someone who truly loves programming will not indulge (much) in the “my paradigm/tool/framework is better than yours” game. A better approach, that will serve you throughout your career, is: “This is the best tool I know for the job right now, and I’m open to improving.”
Just like any other worthy discipline—say music or martial arts—the real master knows there’s always more to discover. In this case, the practice just happens to be building server-side JavaScript apps with Node.