NodeJS Flashcards

1
Q

what is nodejs?

A

it’s a javascript runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is a main difference between nodejs and php?

A

nodejs actually writes the server like listening but php just writes the code that executes on the server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the function u use to import files into a file?

A

require

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a core module we use to make a server?

A

http

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is an architecture that node heavily depends on?

A

event driven structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how many threads does node use?

A

it’s one threaded process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is an event loop?

A

it keeps running as long as event listeners are registered

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the general concept of node?

A

when x happens do y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to create a server?

A

require http then http.createServer()

then listen on the server server.listen();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to redirect to another page?

A

using response.setHeader(‘location’ , ‘’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how should u end your response when creating a server?

A

response.end()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the request body?

A

it’s a stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is a stream?

A

it’s a continuous collection of data that is sent in chunks so that we can parse each chunk without waiting for it all to come

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to work with streams of data?

A

with buffers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is a buffer?

A

it’s a construct that allows u to work with a chunk of data before it’s actually fully parsed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to use a buffer to read the request body?

A

by registering a listener with the method ‘on’ and the listen for ‘data’ and u take every chunk and then register another listener called ‘end’ and user a ‘Buffer’ to concat all ur chunks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what is the difference between writeFile and writeFileSync?

A

writeFileSync blocks the code but writeFile is asynchronus which is non blocking

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what does event loop handles?

A

handle event callbacks only that are fast operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what handles the heavy and slow code of nodejs like handling large file systems?

A

the worker pool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what is the worker pool?

A

it does all the heavy lifting and it’s detached from the event loop and nodejs and it has different threads and once it’s done it will trigger the callback in the event loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is the event loop?

A

a loop which is started by nodejs and keeps the process running and handles all the callbacks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what is the order of the event loop?

A

in the beginning of each loop it checks if
1 - there is any timer that it should execute
2 - pending callbacks :executes code that is deferred by the poll phase
a - if it should call it
b - or it’s already finished from the worker pool

3 - a poll phase which registers new call backs or it checks if it can finish it or defer it to a pending callback

4 - check phase which executes setImmediate() callbacks

5 - close callbacks which executes all ‘close’ event callbacks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

when can we exit the event loop?

A

if there is no registered event callbacks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

how to export files as a module?

A

1 - module.exports

2 - exports which is a shortcut for module.exports

25
Q

how to install a package to a development env?

A

–save-dev

26
Q

how to install a package globally?

A

-g

27
Q

what are the types of errors?

A

1 - syntax errors
2 - runtime errors
3 - logical errors

28
Q

what is the core concept of expressjs

A

using middlewares

29
Q

what is a middleware?

A

it’s a bunch of funnels that the request goes through and u can hook into them

30
Q

what does the next() function do in app.use() in express.js?

A

it allows the request to jump to the next middle as it goes from top to bottom of the script

31
Q

how to parse bodies in expressJs

A

body-parser module

32
Q

how to serve static files like css?

A

it means that the middle ware can’t handle it as it must be forwarded to the filesystem so we use express.static()

33
Q

what happens when during a request u set a constant with a value?

A

even when u refresh the page or even if u go to another browser u still have the value

34
Q

what are some template engines for nodejs?

A

handleBars , pug , ejs

35
Q

does handleBars run logic in template?

A

no we have to pass the login in

36
Q

what is the difference between handleBars and pug when importing them?

A

for handleBars we first need to use app.engine() to define handleBars after requiring it while pug doesn’t need that as a part of it is already in nodejs

37
Q

does ejs have a layout block like pug and handleBars?

A

no

38
Q

what is MVC?

A

separation of concerns

39
Q

what is models responsible for in MVC?

A

respresent data in ur code and work with your data like fetch or save

40
Q

what is views responsible for in MVC?

A

what the user sees and it’s decoupled from application code

41
Q

what is controllers responsible for in MVC?

A

connects models and views , contains ‘in between logic’

42
Q

explain MVC?

A

it divides the application into 3 things , models , views , controllers where models represent data and are responsible for fetching and saving data and views are responsible for what the user sees and it’s decoupled from application code and controller connects both models and views they are the in between and the routes they execute the code in the controller

43
Q

what are the characteristics of SQL?

A

it has a defined schema meaning we define what each column should be and the data type
it has a relation between tables

44
Q

what are the characteristics of NoSQL?

A

1 - it has no defined schema meaning that each document can have different value
2 - no relation between table (we can make them but it makes queries slow) so we just store the documents duplicated in other tables

45
Q

what is the difference between horizontal and vertical scaling in databases?

A

1 - horizontal: adding more servers which then the process is shared across them and then we merge the data
2 - vertical: adding more CPU power the the already existing server

46
Q

which is better for SQL and NoSQL in horizontal and vertical scaling?

A

1 - Horizontal for SQL is extremely hard but vertical is possible
2 - Horizontal and vertical scaling for NoSQL is possible

47
Q

what is the package we use for mysql database?

A

npm install mysql2

48
Q

why do we use a pool when we use mysql db?

A

because each query uses a new connection to the database which is expensive operation

49
Q

what is a database connection pool?

A

the pool keeps a number of open connection to the database(authenticated , authorized) and when we ask for a new connection instead of actually making it we just take from the pool and when we call close it doesn’t actually close the connection but rather moves it to the pool

50
Q

do we use a callback with mysql?

A

we prefer to use a promise

51
Q

what are the 2 main functions of a promise? and why do we use it?

A

‘then’ => catches the callback ,
‘catch’ => called upon error
it makes the code more structured

52
Q

what is sequelize?

A

it’s an object relational model

ORM

53
Q

what is an ORM?

A

is a technique that lets you query and manipulate data from a database using an object-oriented paradigm.

54
Q

how to create a model with sequelize?

A

sequelize.define();

55
Q

how to create the tables in the database using sequelize?

A

sequelize.sync();

56
Q

how to insert a new record using sequelize?

A

1 - sequelize.create(); which creates an object and inserts it automatically
2 - sequelize.build(); which just creates a javascript object then we have to insert it manually

57
Q

how to save a model using sequelize?

A

first get an instance and then call .save() on it

58
Q

how to return a promise from a promise if we have an object?

A

Promise.resolve();

59
Q

how to simulate relations in mongoDb?

A

1 - embedded documents

2 - reference