1 thought on “How could I use `return ‘ok’` instead `res.send(‘ok’)`

  1. @wxs77577 one way is to add a small “sugar”-method yourself to add routes that returns the result. I personally use something like this in a few servers:

    const express = require('express')
    
    /* ... */
    
    const app = express()
    
    function route (method, path, handler) {
      app[method](path, (req, res, next) => {
        handler(req)
          .then(result => result ? res.json(result) : res.status(204).end(''))
          .catch(next)
      })
    }
    
    route('get', '/v1/version', async () => ({ version: packageInfo.version }))
    
    route('post', '/v1/users', userCtrl.post)
    
    route('post', '/v1/login-sessions', loginCtrl.initiate)
    route('post', '/v1/login-sessions/:sessionId/finalize', loginCtrl.finalize)
    
    /* ... */

Comments are closed.