NodeJS Flashcards

1
Q

What can Node do which old JS can’t?

A

It can interface with a computer’s hardware, which is why Electron uses node as an example and why it can act as a webserver.

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

How can I enable ES 6 imports in Node

A

Set the type property in your package.json to module. Typescript can do it by default, as it’s one of its features.

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

What is commonJS

A

It’s node’s way of importing other files into a different file by using require('./whatever) - extension not required.

In the other file use module.exports = ‘anything’

Anything as a variable:

module.exports = 5;

const number = require(‘./module’);

OR

module.exports = {
someFunction(){

}
}

const {someFunction} = require(‘./someModule’);

someFunction();

const someModule = require(‘./someModule’);
someModule.someFunction()

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

What does a bare-bones express app require

A

//import express
const express = require(‘express’);

OR import with type=’module’

import express from ‘express’;

const app = express();

app.get(‘/’,(req,res)=>{res.send(‘some content’)})

app.listen(‘3000’);

SETUP body-parser in order to get the POST body from post requests. - makes req.body available

const bodyParser = require(‘body-parser’);

//middleware

app.use(bodyParser.urlencoded({extended:true}));

REMEMBER
Form data is parsed as strings when sent via HTML form

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

Different res methods

A

These set the response Content-Type header to application/json or text/html.

Second arg is the status code for example 404 - but deprecated

Using res.status(300).send(‘sdffds’) now preferred

res.json() - json
res.send() - html
res.sendFile() - set file path
res.render() - dynamic rendering with ejs for example

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

What is __dirname and it’s purpose

A

It’s to work with relative file paths.
__dirname refers to the absolute path of the folder of the script that’s executing it.

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

When is __dirname not available?

A

In ES6 module files, ie, when type in package.json is set to module

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

What does req contain?

A

req.path - any path variables defined with app.get(‘/blablabla/:someVar/’). Ideal for slugs.

req.body - if body parser is set up

req.query - when query parameters added - users?name=albert

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

How to catch any path not defined and what is important?

A

With wildcard

It should be at the end of the router script, otherwise sub-sequent paths will not be reached.

app.get(‘,()=>{});
app.post(‘
’,()=>{})
app.put(‘’,()=>{})
app.patch(‘
,()=>{})
app.delete(‘*’,()=>{})

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

Explain HTTP methods/verbs

A

POST - to update
PUT - replace data
PATCH - update data
DELETE - removed data
GET - retrieve data

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

What is a popular template engine?

A

EJS

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

What is a template engine?

A

It makes it easy, and less dirty, to dynamically generate HTML, with variable values and conditionals

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

How do you configure a public path for static files

A

This is for serving static items like javascript, images, CSS etc

app.use(express.static(‘public’))

Now all files in the public directory are accessible:

public/styles.css

<link></link>

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

What is middleware in Express

A

Use app.use() to set middleware

It is a function that is run between the request being received and the response being sent back. It is fired even before the route function.

It allows you to for example do authentication and authorization of routes.

It also allows body-parser for example to intercept the request before it reaches the routes.

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

How to implement middleware function

A

app.use((req,res,next)=>{
// call next to go to next middleware or eventually the route if there is no subsequent middleware

next();
})

If next() is not invoked or a response is not returned, it will hang until the request times out.

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

Best practice for authentication and authorization

A

Create middleware for each role, for authentication

Then apply the middleware to the routes/route handlers you want to target via the second arg

app.get(‘/’,middleware,(req,res)=>{

})

or route groups

const myGroup = express.Router();

myGroup.use(someMiddlewareFunction);

myGroup.post(‘/’,(req,res,next) =>res.send());

app.use(‘/a-prefix’,myGroup)

OR

app.use(myGroup)

17
Q

What are route handlers

A

Handles routes, for example

app.get()
app.post(‘/users’,()=>{})

18
Q

How to create route groups

A

//useful middlware

const someGroup = express.Router();

someGroup.get();

app.use(‘/some-prefix’,someGroup);

19
Q

How to create route groups

A

//useful middleware

const someGroup = express.Router();

someGroup.get();

app.use(‘/some-prefix’,someGroup);

20
Q

Writeable and readable streams

A

Readable streams - can read data in chunks from a readable stream.
Writeable stream - can write chunks of data to a writable stream. This can be a file or the browser, via the response object also being a writable stream.

The readable stream fills up a buffer, which in turn sends the filled buffer’s data to the stream.on(‘data’) event.

const stream = fs.createReadStream(__dirname+’/example.txt’,’utf-8’);
const write = fs.createWriteStream(‘without-pipe.txt’)
const writePipe = fs.createWriteStream(‘with-pipe.txt’)

stream.on('data',chunk=>{
    console.log(chunk);
    write.write(chunk);
})

// shorthand

stream.pipe(writePipe);
stream.pipe(res);
21
Q

What is pipe used for?

A

You can pipe a readable stream into a writable stream.

readable.pipe(writable)

22
Q

What are buffers?

A

A buffer is a temporary storage place for data that is being moved from one point to another. Using .toString() on the buffered data will convert it to text.