An Interest In:
Web News this Week
- March 1, 2024
- February 29, 2024
- February 28, 2024
- February 27, 2024
- February 26, 2024
- February 25, 2024
- February 24, 2024
Implementing NodeJs
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 its 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 amimalvar animal2 = animals(); // animal2 stores a name of an amimalconsole.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)
Original Link: https://dev.to/rakshith_2209/implementing-nodejs-314c
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To