I’d like to think I know Node pretty well. I haven’t written a site that use it for about 3 years now. But I’ve never actually sat down and read the docs. web doesn’t As long-time readers will know, I am on a journey of writing out every interface, prop, method, function, data type, etc related to web development, so that I can fill in the gaps of what I don’t know. The Node docs were my last stop, having finished HTML, DOM, the Web APIs, CSS, SVG, and EcmaScript. This held the most unknown gems for me, so I thought I’d share them in this little listicle. I will present them in descending order of appeal. As I do with my outfits when I meet someone new. The querystring module as a general purpose parser Let’s say you’ve got data from some weirdo database that gives you an array of key/value pairs in the form . Naturally you think this might be nice as a object. So you create a blank object, then you create an array by splitting the string on , loop over it, for each item, split it again on and add the first item in that array as a prop of your object setting the second as the value. name:Sophie;shape:fox;condition:new JavaScript ; : Right? No! You use querystring const weirdoString = `name:Sophie;shape:fox;condition:new`;const result = querystring.parse(weirdoString, `;`, `:`); // result:// {// name: `Sophie`,// shape: `fox`,// condition: `new`,// }; _By default, percent-encoded characters within the query string will be assumed to use UTF-8 encoding. If an alternative…_nodejs.org Query String | Node.js v7.0.0 Documentation V8 Inspector Run node with , it will tell you a URL. Paste that URL into Chrome. Boom, debugging Node in the Chrome DevTools. Happy days. There’s a . --inspect how-to by Paul Irish over here It’s still ‘experimental’, but so is my medication and I’M DOING FINE. _Node.js includes a full-featured out-of-process debugging utility accessible via a simple TCP-based protocol and built…_nodejs.org Debugger | Node.js v7.0.0 Documentation The difference between nextTick and setImmediate As with so many things, remembering the difference between these two is easy if you imagine they had different names. should be . process.nextTick() process.sendThisToTheStartOfTheQueue() should be called . setImmediate() sendThisToTheEndOfTheQueue() (Unrelated: I always thought that in React, should be called and should be called . The fact that they’re the same length is just a bonus.) props stuffThatShouldStayTheSameIfTheUserRefreshes state stuffThatShouldBeForgottenIfTheUserRefreshes _Stability: 3 — Locked The timer module exposes a global API for scheduling functions to be called at some future period…_nodejs.org Node.js v7.0.0 Documentation _A process warning is similar to an error in that it describes exceptional conditions that are being brought to the user…_nodejs.org process | Node.js v7.0.0 Documentation _I am pleased to announce a new stable version of Node. This branch brings significant improvements to many areas, with…_nodejs.org Node v0.10.0 (Stable) Server.listen takes an object I’m a fan of passing a single ‘options’ parameter rather than five different parameters that are unnamed and must be in a particular order. As it turns out, you can do this when setting up a server to listen for requests. require(`http`).createServer().listen({port: 8080,host: `localhost`,}).on(`request`, (req, res) => {res.end(`Hello World!`);}); This is a sneaky one, because it’s not actually listed in the docs for , you will only find it in the documentation (which inherits from). [http.Server](https://nodejs.org/api/http.html#http_class_http_server) [net.Server](https://nodejs.org/api/net.html#net_server_listen_options_callback) http.Server _Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the…_nodejs.org net | Node.js v7.0.0 Documentation Relative paths The path you pass to the module methods can be relative. It’s relative to . This is probably something that most people already knew, but I always thought it had to be a full path. fs process.cwd() const fs = require(`fs`);const path = require(`path`); // why have I always done this...fs.readFile(path.join(__dirname, `myFile.txt`), (err, data) => {// do something}); // when I could just do this?fs.readFile(`./path/to/myFile.txt`, (err, data) => {// do something}); _birthtime “Birth Time” — Time of file creation. Set once when the file is created. On filesystems where birthtime is…_nodejs.org File System | Node.js v7.0.0 Documentation Path parsing One of the many things I’ve unnecessarily fiddled with regexes for is getting filenames and extensions from paths, when all I needed to do was: myFilePath = `/someDir/someFile.json`;path.parse(myFilePath).base === `someFile.json`; // truepath.parse(myFilePath).name === `someFile`; // truepath.parse(myFilePath).ext === `.json`; // true _Stability: 2 — Stable The path module provides utilities for working with file and directory paths. It can be accessed…_nodejs.org Node.js v7.0.0 Documentation Logging with colors I’m going to pretend I didn’t already know that will print the object with props/values as different colors, making them much easier to read. console.dir(obj, {colors: true}) _The console functions are usually asynchronous unless the destination is a file. Disks are fast and operating systems…_nodejs.org Console | Node.js v7.0.0 Documentation You can tell setInterval() to sit in the corner Let’s say you use to do a database cleanup once a day. By default, Node’s event loop won’t exit while there is a pending. If you want to let Node sleep (I have no idea what the benefits of this are) you can do this. setInterval() setInterval() const dailyCleanup = setInterval(() => {cleanup();}, 1000 * 60 * 60 * 24); dailyCleanup.unref(); Careful though, if you have nothing else pending (e.g. no http server listening), Node will exit. _Stability: 3 — Locked The timer module exposes a global API for scheduling functions to be called at some future period…_nodejs.org Node.js v7.0.0 Documentation Using the signal constants If you enjoy killing, you may have done this before: process.kill(process.pid, `SIGTERM`); Nothing wrong with that. If no bug involving a typo had ever existed in the history of computer programming. But since that second parameter takes a string the equivalent int, you can use the more robust: or process.kill(process.pid, os.constants.signals.SIGTERM); IP address validation There’s a built in IP address validator. I have written out a regex to do exactly this more than once. Stupid David. will return . require(`net`).isIP(`10.0.0.1`) 4 will return . require(`net`).isIP(`cats`) 0 Because cats aren’t an IP address. If you haven’t noticed, I’m going through a phase of using only backticks for strings. It’s growing on me but I’m aware it looks odd so I’m mentioning it and quite frankly wondering what the point of mentioning it is and am now concerned with how I’m going to wrap up this sentence, I shou _Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the…_nodejs.org net | Node.js v7.0.0 Documentation os.EOL Have you ever hard-coded an end-of-line character? Egad! Just for you, there is (which is on Windows and elsewhere). will ensure your code behaves consistently across operating systems. os.EOL \r\n \n Switching to os.EOL Edit: I didn’t give this enough thought. and have both pointed out in the comments that this can be troublesome. You must assume that any given file could use CRLF( ) or LF ( ) and you simply don’t know. Andrew Meyer Damon Gant \r\n \n If you have an open source project and want to enforce a certain line feed style, there is an that can partially help with this. Doesn’t help if git then messes with it though. eslint rule There is still a use for though. For example when writing out log files that won’t be transported to other OSes. In this case it ensures that the logs files are displayed correctly (e.g. when opened in notepad on a Windows server). os.EOL const fs = require(`fs`); // hard-coded CRLFfs.readFile(`./myFile.txt`, `utf8`, (err, data) => {data.split(`\r\n`).forEach(line => {// do something});}); // based on OSconst os = require(`os`);fs.readFile(`./myFile.txt`, `utf8`, (err, data) => {data.split(os.EOL).forEach(line => {// do something});}); _{ model: ‘Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz’, speed: 2926, times: { user: 252020, nice: 0, sys: 30340, idle…_nodejs.org OS | Node.js v7.0.0 Documentation Status code lookup There is a lookup of common HTTP status codes and their friendly names. is the object of which I speak, where each key is a status code, and the matching value is the human readable description. http.STATUS_CODES So you can do this: someResponse.code === 301; // truerequire(`http`).STATUS_CODES[someResponse.code] === `Moved Permanently`; // true _The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally…_nodejs.org HTTP | Node.js v7.0.0 Documentation Preventing unnecessary crashes I always thought it was a bit ridiculous that the below would actually stop a server: const jsonData = getDataFromSomeApi(); // But oh no, bad data!const data = JSON.parse(jsonData); // Loud crashing noise. To prevent silly things like this from ruining your day, you can put right at the top of your Node app. process.on(`uncaughtException`, console.error); Of course, I’m a sane person so I use and wrap everything in if I’m being paid, but for personal projects… PM2 try...catch Warning, this is , and in a large complex app is probably a bad idea. I’ll let you decide if you want to trust some dude’s blog post or the official docs. not best practice _A process warning is similar to an error in that it describes exceptional conditions that are being brought to the user…_nodejs.org process | Node.js v7.0.0 Documentation Just this once() In addition to , there is a method for all EventEmitters. I’m quite sure I’m the last person on earth to learn this. So … that’s everyone then. on() once() server.once(`request`, (req, res) => res.end(`No more from me.`)); _Much of the Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds…_nodejs.org Events | Node.js v7.0.0 Documentation Custom console You can create your own console with and pass in your own output streams. new console.Console(standardOut, errorOut) Why? I have no idea. Maybe you want to create a console that outputs to a file, or a socket, or a third thing. _The console functions are usually asynchronous unless the destination is a file. Disks are fast and operating systems…_nodejs.org Console | Node.js v7.0.0 Documentation DNS lookup A little birdy told me that Node . So if you’re hitting a URL again and again you’re wasting valuable milliseconds. In which case you could get the domain yourself with and cache it. Or someone else created earlier. doesn’t cache DNS lookups dns.lookup() here’s one dns.lookup(`www.myApi.com`, 4, (err, address) => {cacheThisForLater(address);}); _2) Functions that connect to an actual DNS server to perform name resolution, and that always use the network to…_nodejs.org DNS | Node.js v7.0.0 Documentation The module is a minefield of OS quirks fs If, like me, your style of writing code is read-the-absolute-minimum-from-the-docs-then-fiddle-till-it-works, then you’re probably going to run into trouble with the module. Node does a great job of ironing out the differences between operating systems, but there’s only so much they can do, and a number of OS differences poke through the ocean of code like the jagged protrusions of a reef. A reef that has a minefield in it. You’re a boat. fs Unfortunately, these differences aren’t all “Window vs The Others”, so we can’t simply invoke the “pffft, no one uses Windows” defence. (I wrote a whole big rant about the anti-Windows sentiment in web development but deleted it because it got so preachy even I was rolling my eyes at myself.) Here is a spattering of things from the module that could bite you in your downstairs area: fs The property of the object returned by will be different on Windows and other operating systems (on Windows they may not match the file mode constants such as ). mode fs.stats() fs.constants.S_IRWXU is only available on macOS. fs.lchmod() Calling with the parameter is only supported on Windows. fs.symlink() type The option that can be passed to works on macOS and Windows only. recursive fs.watch() The callback will only receive a filename on Linux and Windows. fs.watch() Using on a directory with the flag will work on FreeBSD and Windows, but fail on macOS and Linux. fs.open() a+ A parameter passed to will be ignored on Linux when the file is opened in append mode (the kernel will ignore the position and append to the end of the file). position fs.write() (I’m so hip I’m already calling it macOS when OS X has only been in the grave for 49 days.) _birthtime "Birth Time" - Time of file creation. Set once when the file is created. On filesystems where birthtime is…_nodejs.org File System | Node.js v7.0.0 Documentation The net module is twice as fast as http Reading the docs, I learnt that the module is a thing. And that it underpins the module. Which got me thinking, if I wanted to do server-to-server communication (as it turns out, I do) should I just be using the module? net http net Networking folk will find it difficult to believe that I couldn’t intuit the answer, but as a web developer that has fallen ass-first into the server world, I know HTTP and not much else. All this TCP, socket, stream malarkey is a bit like to me. Which is to say, I don’t really get it, but I’m intrigued. Japanese rap To play and compare, I set up a couple of servers (I trust you’re listening to Japanese rap right now) and fired some requests at them. End result, handles ~3,400 requests per second, handles ~5,500 requests per second. [http.Server](https://nodejs.org/api/http.html#http_class_http_server) [net.Server](https://nodejs.org/api/net.html#net_class_net_server) It’s simpler too. This is the code, if you’re interested. If you’re not interested then I’m sorry I’m making you scroll so much. _Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the…_nodejs.org net | Node.js v7.0.0 Documentation REPL tricks If you’re in the REPL (that is, you’ve typed and hit enter in your terminal) you can type and it will load that file in (for example you might load a file that sets a bunch of constants). node .load someFile.js You can set the environment variable to disable writing the repl history to a file. I also learnt (was reminded at least) that the REPL history is written to , if you’d like a trip down memory lane. NODE_REPL_HISTORY="" ~/.node_repl_history is a variable that holds the result of the last evaluated expression. Mildly handy! _ When the REPL starts, all modules are loaded for you. So, for example, you can just type so see what your architecture is. You don’t need to do . (Edit: OK to be precise, modules are loaded on demand.) os.arch() require(`os`).arch(); _Edit description_nodejs.org Node.js v7.0.0 Documentation Thanks for reading! Enjoy your day and if I don’t see you before, have a great Christmas. is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising &sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories