Code Monkey home page Code Monkey logo

udemy-nodejs-complete-guide-to-build-restful-apis's Introduction

Seção 01 - Getting Started

What is Node

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;

Node Architecture

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

How Node Works

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.


Seção 02 - Node Module System

Events Module

* A signal that something has happened * Class called http and has the event request, that is listened by the module HTTP.

Seção 03 - Node Package Manager (NPM)

Share modules with NPM. Command do install npm version 5.5.1 npm i -g [email protected]

g for global

Package.json

  • 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)

Installing a node package

* npm install underscore or npm i underscore

Package Dependencies

* mongoose - We use this to store our data in MongoDB

NPM Packages and Source Control

* You do not need the node_modules folder, because all the dependencies are save in package.json. You just need run npm install to install all the modules to node_modules.

Semantic Versioning

  • 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

Listing the Installed Packaged

  • 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

Viewing Registry Info for a Package

* npm view mongoose * npm view mongoose dependencies

Installing a Specific Version of a Package

* npm install [email protected] npm list --depth=0

Updating Local Packages

* npm outdated (Show all the versions who need to update) * npm update * npm i -g npm-check-updates (Install the major verions of a module)

DevDependencies

* npm i jshint --save-dev (Will be save with de properties devDependencie that is not used on production)

Uninstalling Packages

* npm uninstall mongoose * npm un mongoose

Working with global packages

* npm is a example of a global project * npm i -g [email protected] * npm i -g npm * npm -g outdated (All modules are outdated - global)

Publishing a package

* npm adduser * or if you have an account npm login * you need to use a unic name of package name

Updating a Published Package

* You need to update a version of package * npm version major * npm version minor * npm version patch * all the commands to update the version of module

Seção 04 - Building RESTful API's Using Express

RESTful Services

  • 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

Introducing Express

* Express is a Framework to build web applications. * npm i express

Nodemon

* Node monitor * npm i -g nodemon * Use globaly * instead using node index.js we will use nodemon index.js * With nodemon we do not need to reestart the application (hot deploy)

Environment Variables

const port = process.env.PORT || 3000;

Input Validation

* npm i joi * Joi we can define a schema * Joi serves to validate erros and send to front

Seção 05 - Express-Advanced Topics

Introducing

* In this sections * Middleware * Configuration * Debugging * Templating Engines

Middleware

* 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.

Third-party Middleware

* Every Middleware function will impact the performance of your application.

Environments

* export NODE_ENV=production //TO MacOS * set NODE_ENV=production //TO WINDOWS

Configuration

* 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.

Debugging

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

Templating Engines

* All endpoints return json object, but sometimes we need to return html markup to the client and that's where we use a templating engine. * Pug * Mustache * EJS

Database Integration

* You just need install a driver, and then you get a module with a simple API.

Authentication

* Outside of scope of express

Seção 06 - Asynchronous JavaScript

Patterns for Dealing with Asynchronous Code

* There are 3 patterns to lead with asynchronous code * Callbacks * Promises * Async/await

Callbacks

* A callback is a function that we are going to call when the result of an asynchronous operation is ready.

Callback Hell

* When forms a Christmas Tree

Promises

* Holds the eventual result of an asynchronous operation. * When an asynchronous operation completes, it can either result in a value or an error.

Seção 07 - CRUD Operations Using MongoDB

Introducing MongoDB

* A document or NoSQL database.

Connecting to MongoDB

* 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.

Schema

* 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

Seção 08 - Mongoose - Data Validation

Validation

* Mongoose who does the validations. * MongoDB doens't care about validations. * We will use joy for validation. Use Joy and Mongoose Validation.

Seção 09 - Mongoose - Modeling Relationships Between Connected Data

Transaction

* 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

Validating Object ID's

  • npm i joi-objectid

Seção 10 - Authentication and Authorization

Using Lodash

* npm i lodash
  • For complexity passwords use:
  • joi-password-complexity
  • npm i joi-password-complexity

Hashing Passwords

  • npm i bcryptjs

JSON WEB Tokens

*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."

Generating Authentication Tokens

* npm i jsonwebtoken

udemy-nodejs-complete-guide-to-build-restful-apis's People

Contributors

gustavoprimolan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.