Core Modules
Core modules come with Node.js and don't need to be installed. Core modules provide low-level functionality and helper methods. They allow Node.js to work with the filesystem, networking, binary data, streams, spawn external processes, parse query strings, file paths and URLs, and perform other helpful tasks such as creating HTTP(S) agents/clients and servers.
Here's the list of main core modules:
fs
: module to work with the file system, files and folderspath
: module to parse file system paths across platformsquerystring
: module to parse query string datanet
: module to work with networking for various protocolsstream
: module to work with data streamsevents
: module to implement event emitters (Node observer pattern)child_process
: module to spawn external processesos
: module to access OS-level information including platform, number of CPUs, memory, uptime, etc.url
: module to parse URLshttp
: module to make requests (client) and accept requests (server)https
: module to do the same as http only for HTTPSutil
: various utilities including promosify which turns any standard Node core method into a promise-base APIassert
: module to perform assertion based testingcrypto
: module to encrypt and hash information
There is no need to install or download core modules. To include them in your application, all you need is to use the following syntax:
const http = require('http') //replace `http` with the core module you want to use
fs
fs
handles file system operations such as reading to and writing from files. There are synchronous and asynchronous methods in the library. Some of the methods include the following:fs.readFile()
: reads files asynchronouslyfs.writeFile()
: writes data to files asynchronously
Reading from and Writing to the File System in Node.js
Reading from files is done via the core
fs
module. There are two sets of reading methods: asynchronous (recommended) and synchronous. In most cases, developers should use async methods, such as fs.readFile
because this method won't block the event loop:const fs = require('fs')
const path = require('path')
fs.readFile(path.join(__dirname, '/data/customers.csv'), {encoding: 'utf-8'}, function (error, data) {
if (error) return console.error(error)
console.log(data)
})
To write to the file, execute the following:
const fs = require('fs')
fs.writeFile('message.txt', 'Hello World!', function (error) {
if (error) return console.error(error)
console.log('Writing is done.')
})
Full documentation: http://nodejs.org/api/fs.html
Path
path is a core module that is used to work with file and folder paths so that the code works seamlessly on any platform.
path.join()
On Windows paths are separated using a
\
, while on POSIX (Unix, macOS) paths are separated by a /
. Therefore, a path might be app\server.js
on Windows and app/server.js
on POSIX (Unix, macOS). This difference between platforms can cause problems when a path is a hard coded string.
The
path.join()
method is used to create paths that are platform independent. You should use path.join()
instead of hard coding the path because path.join()
is guaranteed to work across platforms.
Example of
path.join()
to create a path to app/server.js
:const path = require('path')
const server = require(path.join('app', 'server.js'))
You can combine path.join with
__dirname
to use an absolute path instead of a relative one:const path = require('path')
const server = require(path.join(__dirname, 'app', 'server.js'))
Full documentation: https://nodejs.org/api/path.html
Ref:
No comments:
Post a Comment