M8 Flashcards

1
Q

What is Node?

A

Node is a JS runtime environment and a library.

It uses the same v8 engine as chrome and has an event loop to handle asynchronous code.

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

Why Node?

A

It’s JavaScript. A language that is already familiar to many developers, so they can use it across the development stack.

The result of the event-driven asynchronous architecture is that Node.js executes quickly.

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

What is Nodemon?

A

Node caches your JS when you start the server, so editing the JS does not update the code that is running on the server.

Nodemon watches for changes to your JS and auto-restarts the server for you.

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

What is npm?

A

Node package manager -

npm includes a command line utility for interacting with the npm repository, i.e.
downloading new packages
managing dependencies

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

What is package.json?

A

Created by npm

This file holds various metadata relevant to the project.

It is used to give information to npm that allows it to identify the project as well as handle the project’s dependencies such as express.

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

What is the –save-dev-flag used for?

A

The –save-dev flag can be used when the package you’re installing is only needed for development purposes, like unit tests or a sass compiler.

Rather than adding the package to the dependencies list in package.json, it gets added to the devDependencies list. The devDependencies list only contains packages needed to develop your package.

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

What is express?

A

Express is a back end web application framework for Node.js. It’s like Slim for Node.

Express is lightweight and unopinionated.

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

What is semantic versioning?

A

Semantic versioning is a set of rules that define how version numbers should be used. It aims to make things consistent between developers.

Given a version number MAJOR.MINOR.PATCH, increment the:

major
May break the API (change how the package is used).
Devs will have to change their code when they use a new version of the package.

minor
Should not break the API.
No code changes should be required to update the package.

patch
Bug fixes.

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

What is routing?

A

Routing connects HTTP verbs and URLs to appropriate code to handle them.

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

What is dynamic routing?

A

Dynamic routes make use of placeholders.

With Express, a placeholder is indicated by a colon followed by a placeholder name.

URLs may also include query strings. I.e. key-value pairs following a ?.

req. query: data from GET URL
req. params: data from route placeholder

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

How do we use Node with MySQL?

A

Using a package called promise.mysql, which wraps function calls with promises.

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

What is MongoDB?

What are Documents, Collections and Fields?

A

Not a relational database.

It is a different way of thinking about database storage: collections and documents.

Document
A JSON object stored in the DB.

Collection
An array of documents - lots of JSON objects.

Field
A key-value pair. Remember JSON consists of key-value pairs.

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

Difference between MySQL and MongoDB?

A

The main difference between putting data into MySQL vs MongoDB is that you are not constrained to a certain schema.
The fields don’t have to be the same for each object.
The DB structure is no longer set in stone.
You just store what you need without having to change table definitions.

Advantage: flexible document schemas. MongoDB’s document model allows virtually any kind of data structure to be modelled and manipulated easily.

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

Mongo DB CRUD operations?

A

Create - db.collection.insertOne/Many()
Read - db.collection.find();
Update - db.collection.updateOne/Many();
Delete - db.collection.deleteOne/Many()

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

What are Modules?

A

A module is a class or group of related functions contained in a file or folder.

Modules help organise and better structure the code of an application.

Modules should be self-contained and reusable.

There are three types of modules in Node:
Core or built-in modules.
Third-party modules.
Custom or local modules.

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

Installing and requiring modules

A

Using npm, we install modules. Third-party modules are put in the folder node_modules.

Installed modules are required in
The require function will look for a file name first, then look for a folder name, then repeat by looking in node_modules.

17
Q

Exporting Modules

A

In Node, a file or a folder can be a module.
Require the folder and it will import from the index.js file.

Use module.exports.functionName = functionName if you need to make the module export multiple functions.

18
Q

What is JEST and why do we use it?

A

JavaScript Unit Testing . A JS testing framework. - VERY VEBOSE

Why?
Ensures correctness of the codebase.
Predictability.
JS is a loosely typed language and unit testing can help ensure that functions/methods are handling data types as expected.

19
Q

How do we write a JEST test?

A

describe(name, fn) creates a block that groups together several related tests in one “test suite”.

it(name, fn) is a unit test itself.

Usually you name it() something like should do something, so that it reads as a natural sentence.

20
Q

Examples of Node Architecture?

A

Micro modules pattern :
Separation of concerns by functionality.
A series of modules that all get used as and when needed.
Works like the node_modules directory - but this can become an unmanageable mess quickly.
Most modules have one main method that is exported.

Controller/services pattern :
Separation of concerns by utility.
Pointing at a controller per entity (rather than a controller per function).
Each entity will have a service (where things get done).

21
Q

What is Middleware?

A

Middleware is code that runs on HTTP requests before a controller and responses after the controller.

In other words, requests and responses pass through the middleware.

22
Q

What tasks do middleware perform?

A

Middleware functions can perform the following tasks:
Execute any code.
Make changes to the request and the response objects.
End the request-response cycle.
Call the next middleware function in the stack.

Middleware is ideal for authorisation and permissions as you want to run the same code for lots of (maybe all) requests.