How to use body-parser

Comprehensive body-parser code examples:

How to use body-parser.text:

181
182
183
184
185
186
187
188
189
  'x-express-openapi-additional-middleware': [validateAllResponses, cleanResponse],
  'x-express-openapi-validation-strict': true
},
consumesMiddleware: {
  'application/json': bodyParser.json(),
  'text/text': bodyParser.text()
},
errorMiddleware: function(err, req, res, next) {
  console.log('app.js_02_Error Middleware:', err);

How to use body-parser.json:

91
92
93
94
95
96
97
98
99
100
101
    console.error(err)
    return res.status(500).json({ error: 'unexpected error' })
  }
})


app.get('/api/game', bodyParser.json(), validateToken, function (req, res) {
  try {
    const actions = determineActions(req.state)
    return res.json({ map: MAP, state: req.state, actions, decryptedToken: req.decryptedToken })
  } catch (err) {

How to use body-parser.urlencoded:

68
69
70
71
72
73
74
75
76
77
78
79
80


app.get('/register', function (req, res) {
  return res.render('register')
})


app.post('/register', bodyParser.urlencoded(), function (req, res) {
  try {
    const { username } = req.body


    const newToken = encryptToken({