If you installed Node.js, you have already have several core modules compiled into the binary. In this tutorial, you will learn how to import the core modules into a local variable with the require function, as well as use a util log function to gather current information on memory statistics.
The require function is what we use to load modules. Many of the modules that we use are hosted the NPM registry and need to be installed first. We'll take a look at those modules later on in the course. For this chapter, and the next few chapters, we're going to focus on those modules that you do not have to install with NPM. These modules were installed locally with your installation of node JS. We refer to these modules as core modules. And path is one of the core modules available to you with node JS. In the last lesson, we used the path module to pluck the base file name from a full path.
We could do that again. I could log the path.base name. And that is a method that we can send a full file path with name to. And we have that available to us on underscore, underscore, file name. And it will pluck out the base file from that full path. Which, in this case, will be core JS. We can also use the path module to create path strings. The path dot join function can be used to join strings together in a path. Ver dirUploads will be a new path that I make to the Uploads directory using the path.join function So, I will join this to the current directory.
And we will assume that it is in a www directory. And then under that, there is a files directory. And then under that, we will assume there is an uploads directory. So, what I have done is I've just created a long path string using the path join function, where I would send it each directory in the path and it will create the string for me. Let's go ahead and log that. So, I will save this and go back to the terminal and run it. And, we can run this just by typing node core.js.
And you can see that the first path.basename plucks the core.js module from the full file path. You can also see that the path.join was used to create a very long path string, that includes the current directory, plus www/files/uploads. So, the path module has some great tools for working with paths. There are other modules that are available to you as a part of the node core. Let's go back to our code. We can use a utilities module.
The utilities module is called util. The utilities module has several helper functions that we can use. One of which is a log. So instead of doing console logs, I can do util logs. And, the difference is we're still going to log this information to the console. The utilitie module log function also adds a date and time stamp. So, I can go ahead and save this, and go back over to the terminal. And we can run our file again. This time I will run it without the JS extension. There should be no difference between doing that.
And here we see that Core JS was logged on the 26th of October at this exact time, and our full path to www, files, uploads was also logged with a time stamp. There are plenty of these modules that we can use. One module you can use to get the current details about memory usage. That's the V8 module. So, I'm going to go back to my code. Since, no JS is built on top of Google Chrome's v8 processor, we can use the v8 module to get information about memory.
So, there are a handful of modules that we can require out of the box with node js. For the next couple chapters, we're going to focus on these modules in particular.
Alex Banks shows how to install Node.js on a Mac or PC and work with the Node.js core: the global object, event loop, http module, and file system. Then he covers reading and writing data, streaming data, making http requests, and working with the node package manager (npm). In the final chapters, he shows how to start sessions on web servers, communicate with web servers and clients with WebSockets, use the Express framework to develop applications, and test and debug Node.js code. Want to speed up your Node.js workflow? Check out the tutorials on automating tasks with Grunt and npm scripts.
- What is Node.js?
- Installing Node.js
- Understanding the global object and global timing
- Importing the core modules
- Handling events
- Creating child processes
- Reading, writing, and removing files
- Working with file streams
- Making http requests
- Serving files and JSON data
- Installing npm, the node package manager
- Working with Express, the web server framework
- Using WebSockets
- Testing and debugging Node.js code
- Working with Grunt and Browserify
- Automating tasks with npm scripts