If there are some conditional breaks, I have to use return res.send('ok'), I wanna just use return 'ok' in every route handlers.
Just like this:
app.get('/', () => 'ok')
Hey @wxs77577, this is not currently possible in express. Like you said in your question, the way is to just res.send.
@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)
/* ... */
Most helpful comment
@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: