Node Js
Node Js
js
Node.js® is a free, open-source, cross-platform JavaScript runtime environment that lets developers create servers, web apps, command line tools and scripts.
Node.js is an
1 open source 2
Node.js is free
server
environment
concurrent requests.
Node.js includes several core modules for essential functionality. Some commonly used ones ar
below:
Example Loose
Type
// Variable store number data type
let a = 35;
console.log(typeof a);
Output
// Variable store string data type number
a = “United College";
string
console.log(typeof a);
boolean
// Variable store Boolean data type
a = true; undefined
console.log(typeof a);
// Variable store undefined (no value) data type
a = undefined;
console.log(typeof a);
Object and
Function
Node.js objects are the same as let company = {
Name: “United College”,
JavaScript objects i.e. the objects
Address: “Prayagraj”,
are similar to variables and it Contact: "+9335833415",
Step Step
1
Create a Folder
Such as WT
2 file java
Create
script file such as
Index.js
index.js
Step Step
D:\WT>
3
Go to Command 4
D:\WT>node
Prompt index.js
Application
Of Node.Js
In this example:
• fs.readFile is asynchronous.
• The last parameter is a callback function that will
be called once the file read operation completes.
Node fs.mkdir() Method
• The fs.mkdir() method in Node.js is used to create a directory
asynchronously.
• Syntax
• fs.mkdir(path, mode, callback)
• Parameters: This method accepts three parameters as mentioned above
and described below:
• path: This parameter holds the path of the directory that has to be created.
• mode: This parameter holds the recursive boolean value. The mode option
is used to set the directory permission, by default it is 0777.
• callback: This parameter holds the callback function that contains an error.
The recursive option if set to true will not give an error message if the
directory to be created already exists.
Node.js fs.rmdir() Method
• The path.dirname() method is used to get the directory name of the given
path. It ignores the respective platform’s trailing directory separators.
• Syntax:
• path.dirname( path )
• Parameters: This function accepts one parameter as mentioned above and
described below:
• path: It is the file path that would be used to extract the directory name. It
throws a TypeError if this parameter is not a string value.
• Return Value: It returns a string with the directory of the path.
• Below programs illustrate the path.dirname() method in Node.js:
Node.js path.extname() Method
components:
• Create server
Install Express:
Create an Application:
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
3. Middleware Usage
4. Error Handling
You can use middleware for various purposes,
such as logging, authentication, or parsing Express has a built-in error handling
mechanism. You can define custom error-
request bodies. For example, to use the
handling middleware
body-parser middleware:
6. Best Practices
• Organize Your Code: Structure your app using separate files for routes, controllers, and
middleware to maintain readability.
• Use Environment Variables: Manage configuration settings using environment variables with
libraries like dotenv.
• Error Handling: Implement comprehensive error handling to improve user experience and
facilitate debugging.
Restful API
REpresentational State Transfer (REST) is an architectural style that defines a set of
constraints to be used for creating web services. REST API is a way of accessing web services
in a simple and flexible way without having any processing.
REST technology is generally preferred to the more robust Simple Object Access Protocol
(SOAP) technology because REST uses less bandwidth, simple and flexible making it more
suitable for internet usage. It’s used to fetch or give some information from a web service.
All communication done via REST API uses only HTTP request.
Key Principles of REST
4.Use of Standard HTTP Methods:
1.Statelessness: •GET: Retrieve resource(s).
•Each request from a client to the server must contain
•POST: Create a new resource.
all the information needed to understand and process •PUT: Update an existing resource.
the request. The server does not store client context. •DELETE: Remove a resource.
2.Client-Server Architecture:
5.Representations:
•The client and server are separate, allowing for
•Resources can have multiple
independent development and scaling. Clients
representations (e.g., JSON, XML).
interact with the API without knowing the server's
JSON is commonly used in RESTful
internal workings.
APIs for data interchange.
3.Resource Identification:
6.Stateless Communication:
•Resources (data entities) are identified by URIs
•Each request is independent, with no
(Uniform Resource Identifiers). For example, /users
session state stored on the server.
might represent a collection of user resources.