node.jz Flashcards

1
Q

In Express, routes takes the following structure: app.METHOD(PATH, HANDLER). METHOD is an http method in lowercase. PATH is a relative path on the server (it can be a string, or even a regular expression). HANDLER is a function that Express calls when the route is matched. Handlers take the form function(req, res) {…}, where req is the request object, and res is the response object.

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

using the get method

A

app. get(‘/’, function(req,res){
res. send(‘Hello Express’)

})

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

using the get method to send a file from index.html

A

app. get(‘/’, function(req,res){
res. sendFile(__dirname + ‘/views/index.html’);

})

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

what is the format of a request

A

HTTP request

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

What is a static website

A

A static website is a website that gives information about a certain website but it cannot be altered with or moved

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

Arrow functions

A

when we use arrow functions we do not use the function keyword

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

Arrow function Syntax

A

The syntax for this is () => { }

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

What is a Promise

A

A promise is a JavaScript object that represents the eventual outcome of an asynchronous operation. aPromise has three different outcomes: pending, fullfilled, and rejected.

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

What does REPl Stand for ?

A

read–eval–print loop

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

os methods

example:
const server = {
  type: os.type(),
  architecture: os.arch(),
  uptime: os.uptime()
}
console.log(server)
A

os. type() — to return the computer’s operating system.
os. arch() — to return the operating system CPU architecture.
os. networkInterfaces — to return information about the network interfaces of the computer, such as IP and MAC address.
os. homedir() — to return the current user’s home directory.
os. hostname() — to return the hostname of the operating system.
os. uptime() — to return the system uptime, in seconds.

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

In Express, you can put in place this functionality using the middleware express.static(path), where the path parameter is the absolute path of the folder containing the assets.

A

app.use(path, middlewareFunction)

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

n Express, you can put in place this functionality using the middleware express.static(path), where the path parameter is the absolute path of the folder containing the assets.

A

Mount the express.static() middleware to the path /public with app.use(). The absolute path to the assets folder is __dirname + /public.

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

using middleWare function

A

app.use((req, res, next) => {
console.log(req.method + “ “ + req.path + “ - “ + req.ip)
next();
})

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

Middleware can be mounted at a specific route using app.METHOD(path, middlewareFunction). Middleware can also be chained inside route definition.

Look at the following example:

app.get(‘/user’, function(req, res, next) {
req.user = getTheUserSync(); // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send(req.user);
});
This approach is useful to split the server operations into smaller units. That leads to a better app structure, and the possibility to reuse code in different places. This approach can also be used to perform some validation on the data. At each point of the middleware stack you can block the execution of the current chain and pass control to functions specifically designed to handle errors. Or you can pass control to the next matching route, to handle special cases. We will see how in the advanced Express section.

In the route app.get(‘/now’, …) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}.

A

app.get(“/now”, function(req,res,next){
req.time = new Date().toString();
next();
},
(req,res) => {
res.send({
time: req.time
})
})

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

Build an echo server, mounted at the route GET /:word/echo. Respond with a JSON object, taking the structure {echo: word}. You can find the word to be repeated at req.params.word. You can test your route from your browser’s address bar, visiting some matching routes, e.g. your-app-rootpath/freecodecamp/echo.

A

app.get(“/:word/echo”, (req,res) => {
const{word} = req.params;

   res.json({
     echo: word
   })
     })
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Add Keywords to Your package.json

A

“keywords”: [“freecodecamp”, “enin”]

17
Q

“package”: “MAJOR.MINOR.PATCH”

A

2.10.2

MAJOR.MINOR.PATCH