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
- Install node.js: here
- Check if it is installed correctly. Type the code in the terminal
node --version
npm --version
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
Browser JS:
- Before the code run, the window object is already created
- The code can access any element shown on the window using
document.querySelector(<classname>)
- Every thing is created on top of that existed window object
Node JS:
- Module based: everything only exist within the code file
- 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")
- Export a function call sayName:
- Path module: provides a lot of very useful functionality to access and interact with the file system. Path module's documentation
- url module
- fs module
- http module
Run code
npm init
in the terminal
- Nodemon: a live server for node.js
nodemon app.js
So every time the app.js file is changed, nodemon will automatically re-run