What is Node js

AhmedAmeer
4 min readApr 2, 2021

Introduction

Node.js is a platform built on Google Chrome’s JavaScript Engine (V8 Engine).And it is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript and node.js was developed by Ryan Dahl in 2009.NodeJS comes with several JavaScript libraries that help basic programming .We use Node.js for most I/O intensive work and we don’t prefer Node.js to CPU intensive work .Node.js doesn’t support multi-threading concept

What Can Node.js Do?

  • can generate dynamic page content
  • can create, open, read, write, delete, and close files on the server
  • can collect form data
  • can add, delete, modify data in your database

Get Start with Node JS

  • First you want to install node js in your system https://nodejs.org official Node.js web site

1. Printing Hello world in the console (platform VsCode)

1.create a folder — -> mkdir nodeExample (Inside VsCode terminal)

2. open the folder which is created before — -> file àopen Folder à select the nodeExample folder

3. switch to the nodeExample folder — -> cd nodeExample (Inside VsCode terminal)

4. Create a file inside the folder — -> nodeExampleà NewFileàapp.js

5. write the code inside the app.js — -> console.log(“Hello World”);

6. Run the code — -> node app.js (Inside VsCode terminal)

2 . Adding two numbers

3. creating a simple http service in nodejs

a).write necessary code in VsCode

b).Run the code

c). In the web browser type the IpAddress and Port Number as localhost:8080

Modules in Node.js

modules are the same as JavaScript libraries(A set of functions you want to include in your application).Node.js has some inbuilt modules like http,fs(file sysytem),crypto.

If we want extra modules or if we want someone else modules we can use NPM (external libraries which are built by someone else)like Mongodb, express.

NPM(Node Package Manager)

It is an external library which is built by some other programmers. We also can create a module and push into the NPM.

To install NPM packages

To use those packages in our project

File System (fs) inbuilt module in Node.js

The Node. js file system module allows you to work with the file system on your computer.

To include the File System module, use the require() method.

EXAMPLES

  1. Read a existing file

We are reading a file (calc.js) which is created in our project and the details of calc.js will be display in our console

  • readFile parameter takes three parameter. First one is name of the file and the second one is encoding and the third one is callback function

2. To Write a file

  • writeFile function will be create a file called newfile.js and the new file consists of some code “console.log(‘new file is created’)”.
  • writeFile parameter takes three parameter first one is name of the file and second one is content which are you want to write and the third one is callback function
  • if we write in the existing file it will replace the entire content ,if we want to add some code in a existing file we can use appendFile function

3. Delete a existing file

Deleting a existing file called newfile.js

Unlink function takes two parameter first one is file name and second one is calback function

--

--