truongductri01 / nodejs_tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NODE.JS

Node.js makes JavaScript run outside of the browser (no need for HTML to see the result returned from the script)

Neither a framework or programming language

SetUp

  1. Install node.js: here
  2. Check if it is installed correctly. Type the code in the terminal
    node --version
    npm --version

Run Code using Node

Create a javascript file, "app.js" for example Inside the app.js, put in the example code: console.log("Hello World")

Then just need to run node app in the terminal

Difference from Node.js and normal Browser Js

Browser JS:

  1. Before the code run, the window object is already created
  2. The code can access any element shown on the window using document.querySelector(<classname>)
  3. Every thing is created on top of that existed window object

Node JS:

  1. Module based: everything only exist within the code file
  2. To use a module, it is needed to be exported and imported
    • Export a function call sayName:
      module.exports = sayName;
    • Import the function (the path to the file may change based on your code's order):
      const sayName = require("./sayName")

Modules covered in the code

  1. Path module: provides a lot of very useful functionality to access and interact with the file system. Path module's documentation
  2. url module
  3. fs module
  4. http module

Should init every project with npm

Run code npm init in the terminal


Dependecy to install

  1. Nodemon: a live server for node.js nodemon app.js
    So every time the app.js file is changed, nodemon will automatically re-run

About


Languages

Language:JavaScript 89.4%Language:HTML 10.6%