0% found this document useful (0 votes)
5 views

Answer 2 Let Us Node

The document outlines the creation of a simple web application using Node.js, focusing on handling HTTP requests and responses, particularly with JSON and HTML. It discusses the use of built-in Node modules like 'fs' for file operations and 'crypto' for cryptographic functions, as well as the installation of additional modules via npm. The document also provides examples of routing and file handling methods in Node.js.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Answer 2 Let Us Node

The document outlines the creation of a simple web application using Node.js, focusing on handling HTTP requests and responses, particularly with JSON and HTML. It discusses the use of built-in Node modules like 'fs' for file operations and 'crypto' for cryptographic functions, as well as the installation of additional modules via npm. The document also provides examples of routing and file handling methods in Node.js.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Let us Node

Let's build a simple Web application that prints Hello World on the browser upon user's request.
We will use the native http module of Node to achieve the Web Server functionality.

One of the most common responses that you need to handle while developing a Node
application is JSON.

npm install --save express


Let’s look over how to send HTML as a response

When you run this code, you should see the following response.

As you can see, we are using switch cases to determine different routes and on each route, we
are sending different responses to each route. Navigate your browser to localhost:3000/home to
view the response
Navigate your browser to localhost:3000/about to view the second response.

Node modules are the basic building block of the Node program. Node module is a set of
functions that can be reused in your application. Node has built-in modules as well as you can
create your own node modules. Some of the famous built-in node modules are fs, net, http and
many of the popular modules such as express, nodemailer built on top of these built-in modules.
We can install node modules using node package manager or called npm. You can install node
modules using the following command. Open your terminal/command prompt and run this
command

You can create your own node module as well. All you need to do is create a function and
export it for the reusability. For example, consider this code which can act as a node module.
There are lots of methods provided under this node module to perform various tasks such as
creating files, writing data into file, reading data from files etc. You can use fs.readFile() or
fs.readFileSync() method to read files in Node. For example: Using the readFile() method.

Using the readFileSync() method.

fs.open() method opens a new file or creates a new empty file if it does not exist in the specified
path. It takes the second parameter which acts as a flag such as w for writing, w+ for reading
and writing etc

fs.appendFile() method appends the provided content at the end of the file.

To delete a file, we can use fs.unlink() method.

You can also copy files using the fs.copy() method.

You should see the following message in the terminal.

Here is the profile page.


Here is the home page.

Here is the login page.

Here is the logout page.

Connected mongodb
Use postgresSQL

redis-server

Redis is read
Node provides a built-in library called ‘crypto’ which you can use to perform cryptographic
operations on data. You can do cryptographic operations on strings, buffers, and streams. To
begin with, install the library if it's not pre-packaged with Node.

Here is the code to generate a hash.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy