Express route needed or not

I use express framework for my webapp. I take some codes from the book, look at this code, this is the route to the page.

app.post('/register', function(req, res) { var firstName = req.param('firstName', ''); var lastName = req.param('lastName', ''); var email = req.param('email', null); var password = req.param('password', null); if ( null == email || email.length < 1 || null == password || password.length < 1 ) { res.send(400); return; } 

What do you need to return here, is it necessary?

+4
source share
1 answer

return is only necessary if you have more code below this point in the route handler function and want to bypass the rest of the function. Nothing in the expression will look or care about the value you are returning. If you are still at the bottom of your function, you can completely omit the return .

Usually you see patterns such as:

  • first do some preliminary checks, checks, authorization or similar logic.
  • If any of this fails, send and execute the error and return from the function to bypass the main logic
  • The main logic code is the following and is executed only if return not been encountered.
+9
source

Source: https://habr.com/ru/post/1481920/


All Articles