Updated March 8, 2023
Introduction to Node Commands
Node is one of the popular open-source platforms, where any kind of JavaScript code can be executed outside of the browser as well. Node is given some specific utility of write some server-side javascript code, which executes before execution of client-side code and displays dynamic content on the screen. Below we are discussing the Node Commands.
Basic Node Commands
There has a lot of Nodes commands, especially for running or handling any kind of client-side and server-side JavaScript code and provide dynamic or proper output to the end-user as per application requirement.
Some of the basic Node commands are mentioned below:
1. HTTP Module
One of the very basic modules of Node is the HTTP module. This is the key module for any kind of node application. This module mainly ensures data should transfer through hypertext transfer protocol (HTTP).
Include HTTP module in the Node JS application; we need to write the below code:
Var http1 = require('http');
- Web Server
One of the big utilities of Node is listening to the corresponding server ports and returning back some responses to the specific client.
That can be done through the below code:
http.createServer1(function (req, res)){
res.write('Executing');
res.end();
}).listen(8082);
This code basically executed when any user tries to access one specific computer through port 8082. The same code can be executed in the command prompt of the computer or can execute in the browser from the specific link (http://localhost:8082).
- HTTP Header
In the case of the HTTP module, a developer can able to add one HTTP header easily by using node JS, which will run in the client system and browser.
Response.writeHead(200, {'content-type': 'text/html'});
Response.write("Messages….");
Response.end();
writeHead method containing the response code (here it is 200, means all is ok in HTML response) and details of the objects hold by the response header.
- Query String
Request parameter of the createServer method actually holding the object of Incoming Message. This object has one specific property name is ‘url.’ Which holds the entire information of the query string, including the domain name.
Response.write(request.url);
The above code always provides an output of printing the specific domain name into the screen. If that domain name additionally holds some information of the query string, it will also display.
2. File System
This module mainly helps the developer for working with a variety of files. It is mainly useful for reading, create, update, delete or rename the file as per requirement. We have to mention the required FS in case of using the file system in our application.
Var filesystem = require('fs');
- Read File
Fs.readFile() is the method having used for reading the files on one computer.
filesystem.readFile('somehtml.html', function(error, data){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
- Create File
There have 3 methods for doing the same properly, filesystem.appendFile(), filesystem.open(), filesystem.writeFile(). Append files are mainly using for appending some text into the file. Open is for opening the file; it normally contains one argument called ‘w’; if it is ‘w,’ then the file has been opened for writing. Write file is the method for replacing existing content with the new one.
- Update File
It is also using the same method appendFile and writeFile for updating one existing file.
- Delete File
filesystem.unlink() method has been used for removing one specific file from the file system.
- Rename File
filesystem.rename() is used for the same.
Intermediate Node Commands
The developer has also used several other popular node commands, which are not very basic commands but work more with node commands to execute these node commands. Some of those kinds of requiring intermediate node commands are mentioned below:
1. URL Module
URL module mainly helping for splitting up the specific web address and provides the output as in readable format. For applying the URL module, require(‘url’) need to be a mention for fetching data from the URL.
- url.parse()
Parsing the URL for finding host, pathname, or context root and query search.
2. NPM Package
NPM is one of the popular package managers for the NODE JS environment. The developer can able to download the same package and use it abruptly in the application properly.
3. Events
Node is mainly configured for working as an event-driven application. Any kind of activity on a computer should consider an event. Suppose the developer wants to open or create one file, then automatically readStream object events fires and provide require output. The event also can consider as a build in the module for Node JS. This means a developer can use require(‘events’) for loading the events object in the application. Suppose developers need to write their own event handler and assign the same to their own performance events, then EventEmitter can be the right option to use. Emit method has been used for firing the event.
Advanced Node Commands
Still, some of the critical tasks need to be done by the node command users frequently. Those tasks also have some advanced kinds of commands that need to be executed, which are mainly used by some of the managerial people for uploading or managing some critical files or handling email fax utility etc. Those advanced kinds of node commands are below:
1. Upload Files
This is one of the key modules for Node JS. This module is called formidable, using mainly for file uploads and all. After downloading the corresponding NPM package, formidable can be declared as ‘require(‘formidable’); ‘ format.
- Upload Form creation
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('<form action="…." method="post" enctype="multipart/form-data">');
response.write('<input type="file" name="…."><br>');
response.write('<input type="UPLOAD">');
response.write('</form>');
return response.end();
- Upload File parsing
var formData = new formidable.IncomingForm();
formData.parse(request, function (err, fields, files) {
res.write('File uploaded successfully');
- Upload File Save
var formData = new formidable.IncomingForm();
formData.parse(request, function (err, fields, files) {
var oldpath1 = files.filetoupload.path;
var newpath = '….' + files.filetoupload.name;
fileSystem.rename(oldpath1, newpath1, function (err) {
if (err) throw err;
response.write('Uploaded and Moved successfully');
}
2. Email
It is mainly handling by the nodemailer module. It is very easy to use for sending mail through the computer.
Require('nodemailer');
Tips And Tricks to use Commands
Some common users who very frequently use node commands, they normally are using some of the tips and tricks for utilizing node commands output in a proper way. Those kinds of tricks normally solving some user-specific queries and display execution output for understanding the same properly.
Some of the very commonly used key tricks are:
- Caching Process: Introducing the caching process in Node will be a very good alternative for speeding up the application and improving the required performance. It basically stored modified or new data in server memory.
- Query Optimization: Whatever we did in the client location, it always depends on some server-side query execution timing. So query optimization is always one of the key points of performance improvement in the NODE JS application.
Conclusion
Node is currently one of the popular javascript-based client sides and as well as server-side programming logic developer has used very frequently. It is very much helpful for those applications where the server-side and client-side both the data are very much required for the presentation.
Recommended Articles
This has been a guide to Node Commands. Here we have discussed basic, intermediate as well as advanced Node Commands. You may also look at the following article to learn more –