Hi,
Will next provide a ability to allow us to embed our own Express.js middlewares / routers for handling server side logic, e.g. backend API.
We can have the following server/body-parser-urlencoded-middleware.js:
import bodyParser from 'body-parser-urlencoded'
export default bodyParser.urlencoded({ extended: false })
and also api-router.js:
import express from 'express'
const router = express.Router()
router.get('/',(req, res) => {
res.send('Hello API')
})
export default router
And for next side will just grabbing all files under server/ directory,
If the filename ends with "middleware" then plug it like app.use(middleware())
If ends with "router" then extract the route, in the case above is app.use("/api", router) and we can request to the API in our next app.
Just a basic concept for open discussion, or is there any other method to implement API feature? Thanks.
+1 on /api
Hi @nodegin
Next.js is (from their README):
A minimalistic framework for server-rendered React applications
I don't think it fit it's purpose to add any middleware or API inside since it's made to create an isomorphic application. You API should be on another server (made with Express for example).
This post seems related to my question. I don't see any way for a next app to handle POST data or ajax requests. If you have to create a separate app to act as an api just to receive writes, that makes a lot of simple projects much more time consuming.
There also doesn't seem to be any way to setup user sessions.
A method to attach an express router above the page router would probably cover all these needs. Any middleware attached to that Router would affect the request/response objects for everything else. You could then attach ajax and post routes to that router and either redirect to get routes or call .next('route') to let the request bubble down to the page router
edit: I see now that Next isn't using express internally, so the request couldn't bubble down. However it looks like it would be fairly easy to add a step to server/index.js#run that invokes an express app with the req/res objects and then checks if a response was sent before invoking the Next router. That doesn't address the need for sessions, however.
@nodegin I guess this can be closed since 2.0.0 is going to include the programmatic api.
Most helpful comment
Hi @nodegin
Next.js is (from their README):
I don't think it fit it's purpose to add any middleware or API inside since it's made to create an isomorphic application. You API should be on another server (made with Express for example).