Skip to main content

Command Palette

Search for a command to run...

Getting Started with Node Js

Updated
β€’5 min read
Getting Started with Node Js
P

Being in love with coding, I'm looking for opportunities to utilize and grow my technical abilities as a programmer (full stack developer, frontend or backend )

Intro to Node Js πŸ”₯

Node Js is a open source run-time environment which uses Chrome V8 Javascript on the Server; created by Ryan Dahl in 2009.

It has capability to run in various platform (MacOs, Windows, Linux). The main purpose of introducing Node js was to build a real-time application with push capability.

🚨 If you want to download Node js in you operating system you can checkout the official documentation and follow the instruction source:Download Node Js

βœ… The main benefits of Node js are:

  • Node helps to run javascript directly in the terminal

  • Due to the node run time, we are able to interact with operating system features from javascript like; access to file, add new file and read the file

  • Thanks to the node js due to which now we can make desktop application(Electron) and mobile application(react-native) as well.

  • Node.js provides developers a comprehensive tool for working in the non-blocking, event-driven I/O paradigm

  • Node Js can be stateless(exposes stateless entities such as; class, objects & methods) or stateful (exposes a stateful instances of an object such as database connection & an instance object of third party service).

image.png Image-source: GeeksForGeeks

Is Node Js is a synchronous or asynchronous πŸ€”?

  • Actually, the question should be is Javascript inside node is synchronous or asynchronous, so the answer is Javascript is still synchronous but it can support asynchronous task because of event queue and event loop.

What is the use of Node js?

  • Node Js helps to build the real-time web application employing push technology over WebSocket. It also helps in two-way connection where both the client and server can initiate communication, allowing them to exchange data more freely.

πŸ“– Tips: In node Js settimeout function working is same but the return type is different

  • Browser: Number

  • Node: Object

What is V8 Engine πŸ›‘ ?

  • V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. πŸ‘‰source: V8 Engine

It is a C++-based open-source JavaScript engine developed by Google which translates JavaScript into executable code i.e. machine code.

Initially, it was created for Google Chrome and Chromium web browsers to enhance the performance of JavaScript execution but latter it executes the js code outside the browser , enabling server-side scripting

To achieve faster JavaScript execution speeds, V8 translates JS code to more efficient machine code instead of using an interpreter. Most modern JavaScript engines like SpiderMonkey or Rhino follow the same approach, but what makes V8 stand out is that it does not produce any intermediate code.

It is a single threaded execution engine. It’s built to run exactly one thread per JavaScript execution context.

You can actually run two V8 engines in the same process β€” e.g. web-workers, but they won’t share any variables or context like real threads. This doesn’t mean V8 is running on a single thread, but it does mean it provides a JavaScript flow of a single thread.

Screen Shot 2022-11-17 at 11.29.22 am.png

What is Express Js ?

  • Express is a one of the most popular node js framework which helps to design and build web application quickly and easily in the server-side into a more organized MVC architecture.

Generally, it is a framework which means the code is already written for programmer to work with. With the help of express we can build single page, multi- page applications

  • Building web application is easy and fast
  • It reduces the amount of bugs you will write
  • Helps to build a REST API Server
  • Allows you to define routes of your application based on HTTP methods and URLs.
  • Easy to connect with databases such as MongoDB, Redis, MySQL

🚨. Installing Express in your project is very easy you just copy the below code

npm install express

creating an express server

  • Step: 1 ==> Make a folder and open it in an IDE (Vs code) and make a file name app.js

  • Step: 2 ==> Open the terminal and write npm init -y { you can see a package.json file}

  • Step: 3 ==> You can simply write npm install express to install the express on your project folder {node_module folder will appear inside you project folder}

  • Step: 4 ==> If you want you can install Nodemon by simply typing npm i nodemon in the terminal {The purpose of this package is to watch for any changes in our files and restart the server instead of us having to do that manually ourselves.}

  • Step: 5 ==> Inside the package.json file add a start script with nodemon app.js

"start" : "nodemon app.js"

πŸ‘‰Now your package.json file looks like this πŸ‘‡

Screen Shot 2022-11-17 at 12.11.44 pm.png

  • Step: 6 ==> Open app.js file and copy the snippet
const express = require('express')
const app = express()
const port = 3000

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
  • πŸ‘† In the above code snippet we are importing express(using Common Js module) and storing in express variable and initialising the express() and storing in app variable. i'm using port 3000 and we are listening the port using app.listen() which takes a callback function and simply printing in the console .

  • Step: 7 ==>After that you can simply add a request and your final code snippet will looks like this πŸ‘‡

Screen Shot 2022-11-17 at 12.24.31 pm.png

  • Step:8 ==> Again, in your terminal write npm start and your terminal look like this πŸ‘‡ Screen Shot 2022-11-17 at 12.26.49 pm.png

  • Congratulation !! πŸ‘ you are almost done... at last go to your browser and write and Hello world will appear

http://localhost:3000/

❀️ Thank you for reading and hopefully I have enticed you to learn more and experiment with Node.js and Express.js.

More from this blog

Untitled Publication

12 posts