Section 1

Preview this deck

Handling errors with promises

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (13)

Section 1

(13 cards)

Handling errors with promises

Front

-

Back

How to read a file with NodeJS

Front

Using the 'fs' module. require the fs module and use the 'readFile' function. const fs = require('fs') fs.readFile('/etc/hosts', 'utf8, function(err, data){ if(err){ return console.log(err) } else { console.log(data) } });

Back

Promises

Front

-

Back

How to create a route in NodeJS

Front

It needs an instance of express and use one of the HTTP verbs (Get, Post, Put, Delete) and execute some code into the handler function. The response object will have different response methods like download file, redirect, send response, send json response.

Back

Event-driven programming

Front

NodeJS uses events heavily. This is one of the reasons why it's pretty fast. As soon as nodeJS starts its server it initiates its variables, declares functions and waits for an event to occur. In an event driven application there is a main loop that listens for events and then triggers a callback function when one of those events is detected. While events seem similar to callbacks: Callbacks are called when an asynchronous function returns its result. Event handling works on the observer pattern. The functions that listen to events act as observers. Whenever an event gets fired its listener functions starts executing.

Back

ECMAScript 6 new features

Front

-

Back

Non-Blocking

Front

It means that the application won't wait until a request returns data to continue the process, the server will move to the next request immediately.

Back

Asynchronous

Front

When a request is executed the server won't wait for a response to be able to execute the next one. It will execute N number of requests and respond to previous requests with the help of the event loop.

Back

NodeJS Architecture

Front

It is build on top of C++ and implements the V8 javascript engine. It handles external actions like communicating with a DBMS or reading a file from a server using callbacks.

Back

Promise states

Front

-

Back

Difference between grunt and gulp

Front

They both are task runners but grunt focuses on configuration and gulp focuses on code. Every task in grunt is an array of different plugin configurations that get executed one after another and almost every task requires a source destination to be specified. This makes grunt relatively slower than gulp. Gulp uses nodeJs streams and is faster because it does not open/close files or creates intermediary copies all the time. Gulp ises plugins that are developed by the community of developers. In gulp you stream one plugin after the other within a task definition.

Back

Callbacks

Front

Callbacks are just the name of a convention for using JavaScript functions. There isn't a special thing called a 'callback' in the JavaScript language, it's just a convention. Instead of immediately returning some result like most functions, functions that use callbacks take some time to produce a result. The word 'asynchronous', aka 'async' just means 'takes some time' or 'happens in the future, not right now'. Usually callbacks are only used when doing I/O, e.g. downloading things, reading files, talking to databases, etc.

Back

Middleware

Front

A block of code that executed between the request until the response gets to the server. They provide a mechanism for filtering HTTP requests entering your application. An example is creating a middleware to grant a logged user access to a URL. const middlewares = { isLoggedIn: function(req, res, next){ if(req.user){ return next(); } res.redirect('/') } } module.exports = middlewares; in the controller: const isLoggedIn = require('middlewares') router.route('/admin').get(isLoggedIn, function(req, res){ ... }) Types of middlewares: - Authentication - CORS (Cross Origin Resource Sharing). - Log In - Routing

Back