Answer 2 Let Us Node
Answer 2 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.
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.
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.
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.