I
I
IDONTSUDO2019-09-22 12:41:29
Node.js
IDONTSUDO, 2019-09-22 12:41:29

Node.js how to exit an application?

I have an endpoint. And two callbacks below.

router.put('/fixtures/change/main/:fixtoId', jwtTokenUserRole,ChangeMainFixture )

exports.jwtTokenUserRole = async (req, res,next) =>{
    
    let head = req.headers

    var token = head.authorization.replace('Bearer ','')
   
    let j = jwt.decode(token)

    req.auth = j.role
    
    next()
}

exports.ChangeMainFixture = (req,res) =>{
    let fixMain = req.fixtur
   
    fixMain = _.extend(fixMain, req.body)
    fixMain.save((err, result) => {
        
        if (err) {
            return res.status(400).json({
                error: err
            })
        }
        
        res.json(result)
    })
}

I want the first callback to define the user's permissions. And if the request was not made by the admin. That did not go further along the calbeks. I found the process.exit(1); function in the documentation. But the Nodejs developers do not recommend using this. Keeping track of rights in the last callback and giving response from there is not an option. I can set it up with a thousand if(), but I would like to find a better solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-09-22
@IDONTSUDO

There is such a thing as middleware, an intermediary that will cut off for the necessary routes according to the desired condition (which is the admin). Write once and add it to the routes, while the elbow - to the route it will come if other layers are skipped.
https://expressjs.com/en/guide/using-middleware.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question