Introduction
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.Node.js was written initially by Ryan Dahl in 2009, about thirteen years after the introduction of the first server-side JavaScript environment, Netscape's LiveWire Pro Web.Node.js is an open-source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux. Node.js also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent. Because of its superfast capabilities, it has been successful in attracting corporate clients like IBM, LinkedIn, and many other giants in the industry.
Lets get Started Shall we?
In this post, we will take a look at how you would be able to incorporate Node.js into your Web Application. You are going to learn the usage of this from scratch as well as take a look at an example.
Prerequisites
- a Web browser such as Chrome.
- Node.js above 12.x.x
- Basics of JavaScript
- NPM install.
- Terminal ( I am using Hyper terminal in this post )
When you install Node it already comes with a bunch of modules. In this post we will take a look at one such built-in module and also we would take a look at how to incorporate external modules. We can use Node to get access to the local files of the computer too.
The link provided here takes us to the official Node.js File System Documentation page..
(https://nodejs.org/api/fs.html)
To illustrate the fact that Node can be used to manipulate the file-systems of your machine we will be taking a look at a method called copyFileSync() which copies the content from one file to the other.
Start with creating a folder called "introduction-to-node" , and creating a Javascript File.
In order to use the modules we first need to require the module. Now also create a simple text file called "file1.txt". Now write any sentence you like...
const fileSystem = require("fs");
// "fileSystem" is our constant and "fs" is the module.
fileSystem.copyFileSync("file1.txt","file2.txt");
implement the same by typing "node index.js"
As you can see we have successfully implemented the file System Module and we have achieved the target of copying contents of one text file to the other. (ie from file1.txt to file2.txt)
How to incorporte external modules using NPM?
NPM stands for Node Package Manager and it’s currently the world's largest collection of packages. Using NPM we could incorporate packages inside your projects saving us time and energy. Make sure that you are in the working directory. Now we are going to initialize NPM here by saying "npm init -y".
Now we will try to install a sample package called "animals" and try experimenting with it.
The link to the same is provided here. (https://www.npmjs.com/package/animals).
We will accomplish this in a very systematic step by step procedure as shown below:-
- Create a new folder.
- Create a new JavaScript file.
- Using Hyper Terminal initialize NPM.
- Install the sample "animal" package by typing "npm install animals"
Now with that in place, we can verify that we have successfully installed the required packages by looking at our dependencies in our package.json file.
In this simple illustration we will try logging the names of animals in the console.
var animals = require("animals");
var animal1 = animals(); // animal1 stores a name of an amimal
var animal2 = animals(); // animal2 stores a name of an amimal
console.log("Name of my first animal is: " + animal1);
console.log("Name of my second animal is: " + animal2);
Now just test your output using the Hyper Terminal.
Conclusion
I hope in this post you get the idea of how to work with Node Packages and incorporate any external modules too.
Link to my twitter: (https://twitter.com/rakshith_2209?lang=en)
Top comments (8)
In my opinion the title "Implementing NodeJS" is misleading as anyone who reads this will think the post talks about the implementation of NodeJS using the ECMAScript specification.
Based on the content of the post and what you said in the second paragraph (emphasis mine):
The post should be titled How to incorporate NodeJS in to your application or better still Getting started with NodeJS and NPM.
I had the same feeling as you. I came to this post expecting an implementation of NodeJS, but it is more like how to use NodeJS to run your Javascript code
Sorry to nitpick, but does anyone else find it funny that the main banner image code has nothing to do with Node.js?
Little may not know, but the cover image should correlate or at least have a connection with the content of the post.
Content writing is not an easy task.
Its nice to still see Node mentioned somewhere, as of current buzz the feeling could be summarized, if you don't use Deno, it doesn't count :)
Yes, sir completely agree with you.
Great tutorial! Keep up the good work 👏
Thank you so much... :)
Some comments have been hidden by the post's author - find out more