Studying Node.js through the NodeBR course.
Versão em Português-BR
- Created by Ryan Dahl in 2009. Node.js is a platform for building backend applications using JavaScript.
- NPM (stands for Node Package Manager) is responsible for managing the dependencies(package.json) of the project.
- Install Node.js in your computer https://nodejs.org/en/.
- Run the command
node --version, to check the version of the node that is installed on your computer.
- To start a node.js project, run the command
npm init(ornpm init -yto run as the default settings).
NPM also can be used to write line commands.
In the package.json file:
"scripts": {
"dev": "node index.js"
},
In this code snippet, it is showed the function "dev".
To execute line commands with NPM it is used the command npm run "name-of-your-function", with base of the code above, it would be npm run dev.
- Functions that need external resources (access to database or files) will be running in the background.
- The code does not run sequentially.
- Remember to keep the sequence of your program to avoid problems.
- Pending: Initial state, not yet finished or not rejected yet.
- Fulfilled: After performing all operations successfully.
- Rejected: Operation failed.
Async The word “async” before a function means that this function always returns a promise. Await it just works only inside async functions and makes JavaScript wait until that promise settles and returns its result.
- It makes it easy for the visualization of the flow of functions.
- It does not change the performance of your application (if used correctly).
- Already existing in C # was developed in JavaScript.
- Use only when you need to wait for the call's answer (Await).
- Used for continuous actions.
- Node.js uses for almost everything in your ecosystem.
- Quite also used in browsers (.onClick).
- It work under Design Pattern Observer / PubSub]
npm init -y-> Start Node.js projectnpm install axios-> Insall Axios Libnpm install -g mocha-> Install Mocha globallynpm i --save-dev mocha-> Install Mocha as a development dependencynpm install nock-> Install the Nock package. It's used to simulate requests