reading-notes2

https://m7madmomani2.github.io/reading-notes2

View the Project on GitHub M7madMomani2/reading-notes2

Node.js

What Is Node and When Should I Use It?

Image

  • Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
  • The V8 engine : is the open-source JavaScript engine that runs in Google Chrome and other Chromium-based web browsers, including Brave, Opera, and Vivaldi. It was designed with performance in mind and is responsible for compiling JavaScript directly to native machine code that your computer can execute Use a version manager.

Image

  • version manager is a program that allows you to install multiple versions of Node and switch between them at will.

  • There are various advantages to using a version manager.
  • You can check that Node is installed on your system by opening a terminal and typing node -v. If all has gone well, you should see something like v12.14.1 displayed. This is the current LTS version at the time of writing. npm, the JavaScript Package Manager
  • To check which version you have installed on your system, type npm -v.
  • npm is also the world’s largest software registry. There are over 1,000,000 packages of JavaScript code available to download, with billions of downloads per week. Let’s take a quick look at how we would use npm to install a package.

Image

server101

we can use node Name.js to run js code and see what’s happened

npm init -y

  • This will create and auto-populate a package.json file in the same folder

npm install lodash –save

  • install the lodash package and save it as a project dependency.
  • you can check dependencyes from package.json file

require(‘http’); :-

  • We use this to create a new web server object,

    http.createServer((request, response):-

  • The anonymous function is called with two arguments (request and response).

    response.writeHead(200); response.end(‘Hello, World!’);

  • These contain the request from the user and the response, which we use to send back a 200 HTTP status code, along with our “Hello World!” message.

    .listen(3000):-

  • we tell the server to listen for incoming requests on port 3000, and output a message to the terminal to let us know it’s running.