- Get link
- X
- Other Apps
What is Node.js?
- Node.js is an open source server framework.
- Node.js is free.
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).
- Node.js uses JavaScript on the server.
- Node.js files are saved with extension '.js'.
What Can Node.js Do?
- Node.js can generate dynamic page content.
- Node.js can create, open, read, write, delete, and close files on the server.
- Node.js can collect form data.
- Node.js can add, delete, modify data in your database.
How to download Node.js?
You can download and install node.js from the official website here. Node.js is cross-platform and can be used in almost all the OS.
Using Node.js:
You can edit node.js with any text editor. But I would recommend you to install and use Visual Studio Code which is cross-platform and also has IntelliSense that leads to faster coding. You have to add some extensions to edit node.js files more efficiently.
Creating Hello World Page:
To create a Hello World page, follow the steps mentioned:
- Create a file with the extension '.js'.
- Edit that file as follows:
- The above code tells the computer to write 'Hello World' if anyone i.e. web browser tries to open the port 8080.
- Node.js files must be initiated with command line interface(CLI).
- Open up your command line: For Windows: Open up Command Prompt. For Mac or Linux: Open up Terminal.
- Navigate to the folder that contains the '.js' file using 'cd' command. For example, my file is located in my Documents so the command will be: 'cd C:\Users\UserName\Documents'.
- Initiate the file using 'node fileName.js'. Here fileName is your file name.
- Do not close the command line interface.
- Now, open up the port which is http://localhost:8080/.
- "Hello World" will be displayed on your web browser.
Note: For Visual Studio Code, there is an integrated Terminal, which can be accessed from View>Integrated Terminal.
What is NPM?
NPM is a package manager for node.js files. It hosts thousands of free packages to be installed for free from their site named https://www.npmjs.com/. NPM is used to install node.js modules for your node.js application. Modules are JavaScript libraries that you can include in your project.
How to install npm?
NPM comes along with Node.js. That means when you have installed Node.js, you have also installed npm alongside.
NPM can only be accessed from the command line using the command 'npm'. You can check whether npm is installed on your computer by entering 'npm -v' which would give you a version number else it is not installed. Installation of a module is done by the command 'npm install package_name'. Here, package_name is the name of the package.
NPM can only be accessed from the command line using the command 'npm'. You can check whether npm is installed on your computer by entering 'npm -v' which would give you a version number else it is not installed. Installation of a module is done by the command 'npm install package_name'. Here, package_name is the name of the package.
Using a package on the Node.js application:
Downloading a package is very easy. Just open the command line interface and tell npm to install a package.
To terminate a node.js process use 'Ctrl-D' or 'Ctrl-C' twice.
In my example, I shall use upper-case package which converts all the letters of a word the uppercase. Follow the steps mentioned below:
- Write the code as follows:
- Save the file with extension '.js'
- Open up CLI and navigate to the directory of the file saved.
- Run the node server:> node fileName.js
- Open up the port http://localhost:8080/.
- All the letters of the word will be capitalised.
- Terminate the server process using 'Ctrl-D'.
Conclusion:
In this blog, we learned how to create a node.js application and the use of server framework with modules.
Comments
Post a Comment