A runtime environment for executing JavaScript code; We often use Node to build back-end services, also called Application Programming Interface. Web App and Mobile App communicate with API
Node is Highly-scalable, data-intensive and real-time apps. Great for prototyping and agile development. Superfast and higly scalable. JavaScript everywhere Cleaner and more consistent codebase Large ecosystem of open-source libs
PayPal use this
A Node APP - Built twice as fast with fewer people; - 33% fewer lines of code; - 40% fewer files; - 2x request/sec; - 35% faster response time;
What is a runtime environment really? Before Node we used javascript only to build applications that run inside a browser.
A browser has a Javascript engine that takes our code and converts into code that a computer can undertand
JavaScript Engines of each Browser:
It's because of these varieties of engines, that javascript codes can behave differently in one browser or another.
In 2009 Ryan Dahl, creator of Node. He took google's v8 engine, which is the fastest JavaScript engine, and embedded it inside a C++ program and called that program Node. Node is a runtime environment for javascript code. Both Chrome and Node share de same Javascript Engine.
Node is not a programming language! Node is not a framework It's a runtime environment for executing JavaScript code
Node is a non-blocking or asynchronous architecture. Node applications are asynchronous by default. Node is ideal for I/O-intensive apps.
Do not use Node for CPU-intensive apps like video encoding or an image manipulation service. In these king of applications, we have a lot of calculations that should be done by CPU, and few operations that touch the file system or the network.
* A signal that something has happened * Class called http and has the event request, that is listened by the module HTTP.
Share modules with NPM. Command do install npm version 5.5.1 npm i -g [email protected]
g for global
-
A json file that includes some basic information about your application or your project.
-
run npm init
-
or run npm init --yes (You dont need to answer the questions about your application)
-
4.13.6
-
MajorVersion.MinorVersion.PatchVersion
-
PatchVersion is for bug fixes
-
MinorVersion is used for adding minor features that don't break the existing API
-
MajorVersion is uded to add a new feature that could potentially break the existing applications
-
^4.13.6 //4.x
-
~1.8.3 //1.8.x
- To see all the versions of Installed Packaged you can see on the package.json file of module or simples run:
- npm list
- npm list --depth=0 //Only the dependencies of your application is using
-
REST is short fr Representational State Transfer.
-
REST is basically a convention for building these http services. We use simple http protocol principles to provide support to create, read, update and delete data. We refer to this operations all together as CRUD operations.
-
HTTP METHODS
- GET - Getting data
- POST - Creating Data
- PUT - Updating Data
- DELETE - Deleting Data
const port = process.env.PORT || 3000;
* npm i joi * Joi we can define a schema * Joi serves to validate erros and send to front* In this sections * Middleware * Configuration * Debugging * Templating Engines * Middleware function is basically a function that takes a request object, and either returns a response to the client, or passes control to another Middleware function. * Every Middleware function will impact the performance of your application. * export NODE_ENV=production //TO MacOS * set NODE_ENV=production //TO WINDOWS * You do not store de password of database and secrets informations on configuration * In file custom-environment-variables.json we define the mapping of configuration settings to environment variables. const startupDebugger = require('debug')('app:startup'); //SET OU EXPORT A VARIÁVEL DEBUG=app:startup const dbDebugger = require('debug')('app:db');
*SET or EXPORT DEBUG=app:startup,app:db or shortcut
- DEBUG=app:db nodemon index.js
* There are 3 patterns to lead with asynchronous code * Callbacks * Promises * Async/await * A callback is a function that we are going to call when the result of an asynchronous operation is ready. * When forms a Christmas Tree * Holds the eventual result of an asynchronous operation. * When an asynchronous operation completes, it can either result in a value or an error.
* A document or NoSQL database. * Mongoose gives us a simple api to work with a MongoDB database. * When you want to deploy your application to a production envirnment, you're going to have a different connection string for the production envirnment. * We use a schema to define the shape of documents within a collection in MongoDB. * Schema Types: * String * Number * Date * Buffer - Which we use for storing binary data * Boolean * ObjectID which is used for assigning unit identifiers * Array
* Mongoose who does the validations. * MongoDB doens't care about validations. * We will use joy for validation. Use Joy and Mongoose Validation.
* In SQL Databases we have the concept of transaction, which basically means a group of operations that should be performed as a unit. So either all these operations will complete and change the state of the database, or if something fails in the middle, all these operations that have been applied will be rolled back and our database will go back int he initate state.
-
Now, in MongoDB we don't have transactions as we have in these relational databases, we have a technique called two phase commit.
-
npm i fawn
- npm i joi-objectid
- For complexity passwords use:
- joi-password-complexity
- npm i joi-password-complexity
- npm i bcryptjs
*JWT
- When the users logins in the server, we generate this JSON Web Token, which is basically like a license or password. We give it to the client, and then tell them: "next time you want to come back here and call one of our PAI endpoints, we need to show your password, we need to show your drivers license."