dadi / web

Web is a drop in front end for websites and web apps. Consumes data from DADI API and others
https://dadi.cloud/en/web/
Other
48 stars 16 forks source link

page responds to GET and POST #409

Closed abovedave closed 3 years ago

abovedave commented 6 years ago

Currently page's take precedence over middleware. Page

{
  "page": {
    "name": "form"
  },
  "routes": [
    {
      "path": "/form"
    }
  ]
}

Middleware

const Middleware = function (app) {
  app.use('/form', (req, res, next, refresh) => {
    if (req.method.toLowerCase() !== 'post') return next()

    console.log('App never gets here on POST...')
  })
}

module.exports = function (app) {
  return new Middleware(app)
}

module.exports.Middleware = Middleware

Couple of options:

jimlambie commented 6 years ago

1) when we load each of the routes specified in page.json files, we use app.use which allows any method through. GET is handled by default in the controller, POST requests must be handled by events. We could add a property to a route that indicates the method it corresponds to, and deal with that somewhere in the route validation function.

2) we can certainly implement methods for each verb, that's easy enough to do. That would mean in middleware we could do:

  app.post('/foo/post', (req, res, next) => {
    return res.end(JSON.stringify(req.body))
  })

  app.get('/foo/get', (req, res, next) => {
    return res.end(JSON.stringify(req.params))
  })

This would allow middleware to handle a POST, rather than an event. However, the middleware doesn't have access to page data, as the request knows nothing about events or datasources and never goes through the controller - because we're loading a route + handler that is unrelated to any page specification.

@abovedave what is the refresh argument you've added? A function with 4 arguments is treated as an error handler.

abovedave commented 6 years ago

Yes I think it was the refresh breaking it 🙈

Still, would be useful to have get and post like that for shared routing