Getting Started with node.js
What is Node.js ?
- Node.js is an open source server environment
- Node.js is free
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
- Node.js uses JavaScript on the server
Why Node.js?
- Because Node.js uses asynchronous programming!
History of Node.js
Node.js was originally written by Ryan Dahl in 2009. Nearly thirteen years after the introduction of the first client-side JavaScript environment, Netscape’s Liveware Pro Web. Initial release was supported only by Linux and Mac OS X. Its development and maintenance was led by Joyent.
Downloading and Installing Node.js
Installing Node.js is very easy. Just go to the Node.js website, select the corresponding OS, and follow the guide and install it your device
To test the installation successfully, open the terminal and test the version as follows:
go to the device command prompt and type -: node -v
You can see the node version of your device as shown in the image above.
Note: The version number may be different
Check the npm version as well then go to the device command prompt and type -: npm -v
You can see the npm version of your device as shown in the image above.
Note: The npm version number also may be different
Let’s see how to print based on “Hello World” console and web server.
Example: Console-based
Use the Node.js console module to print the output of your system console. Create an example.js JavaScript file using the content below.(I’m using Visual Studio Code, but any editor you can use.) then save it .
after that take terminal and go to the right folder location and type command node example.js
After command is running successfully you will see the console log message in your terminal.
Example: Web server-based
A Node.js web application is build with 3 parts.
- Import module to create web server
- Create a web server
- Read client requests and send response back to client
To start really simply, let’s steal a simple example from the Node.js site. Copy the code and paste it into your code editor , then save it as example.js.
after that take terminal and go to the right folder location and type command node example.js
After command is running successfully you will see the console log message in your terminal. Then take that url and open it in your web browser.
You can see the “Hello world” Message of your web browser as shown in the image above.
Press Control-C in the Terminal to stop the Node server.
Congratulations!!! You have successfully created your first program using node.js.
Let’s look at the pros and cons of Node.js
Pros of Node.js
- Single programming language.
- More fast deployment.
- Improved developer efficiency and overall productivity.
- Scalability.
- Sharing and reusing code.
- It is simple to share knowledge within a team.
Cons of Node.js
- Due to the unstable API, significant code changes have occurred.
- lack of library support.
- There is a high demand for experienced NodeJS developers and there are only a few.
- Code difficult to maintain.
- There is a high demand for experienced NodeJS developers and there are only a few.
Time to say Goodbye!!! and I hope to see you again with my new blog.