N
N
narem2019-09-05 14:58:51
JavaScript
narem, 2019-09-05 14:58:51

What does .chance mean?

const express = require('express')
const app = express()
app.use((request, response, next) => {  //Промежуточный обработчик
    console.log(request.headers)
    next()
})
app.use((request, response, next) => {
    request.chance = Math.random()
    next()
})
app.get('/', (request, response) => {
    response.json({
        chance: request.chance
    })
})
app.listen(3000)

What does .chance and chance: request.chance, .headers mean within this code?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2019-09-05
@sergiks

chanceis a new invented property of the object requestassigned to it by the handler.
A request has arrived. A new property chancewas attached to it, writing a random number from 0 to 1 into it.
In response to a GET request, JSON is returned describing an object with a single property, also called "chance" (chance), with a value taken from the previously assigned property of the request object.
You can name the property whatever you like. For example, toster. Then the code would look like this (replacement in two places), but function exactly the same; returned the same response format:

the code
const express = require('express')
const app = express()
app.use((request, response, next) => {  //Промежуточный обработчик
    console.log(request.headers)
    next()
})
app.use((request, response, next) => {
    request.toster = Math.random()
    next()
})
app.get('/', (request, response) => {
    response.json({
        chance: request.toster
    })
})
app.listen(3000)

.headersis a property of the request object inherited from the native Node request object, see http.IncomingMessage.headers . Contains all HTTP headers of the received HTTP request.

X
xonar, 2019-09-05
@xonar

Judging by the code, it looks like some kind of game system, where .chance gets a random chance, and all other chances seem to be recalculated below. Huh, probably not right.
Only one thing came to mind - https://coub.com/view/7fzfq

P
profesor08, 2019-09-05
@profesor08

Look at the code where this value is used. If not, you can delete it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question