I'm wondering if the following scenario is possible with serverless (deployed on Vercel) Next JS:
I have a route /product/[id].tsx
when you send a request with the header Accept: text/html
, I would like it to go through the normal Next JS flow with a React page. But, when a request is given with Accept: application/json
, I would like it to return the JSON representation.
I have accomplished it by using some custom middleware I wrote for Express (see below), but I would also like to deploy it on Vercel, and Vercel is not designed to work with a custom Express implementation.
So the question is, is it possible to do this in a serverless environment? Could one of the Next JS React pages return pure JSON, or could you return React from a Next JS Api route? Or is there another way to accomplish this?
server.ts
import express, { Request, Response } from "express";
import next from 'next'
import { parse } from 'url'
const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
async function run(): Promise<void> {
const app = express();
const nextApp = next({ dev })
const nextHandler = nextApp.getRequestHandler()
await nextApp.prepare()
app.use((req, res) => {
res.format({
"text/html": async (req, res) => {
const parsedUrl = parse(req.url!, true)
await this.handler(req, res, parsedUrl)
},
"application/json": async (req, res) => {
res.json({
"dummy": "data"
})
}
})
});
app.listen(port, () => {
console.log(
`> Server listening at http://localhost:${port} as ${
dev ? "development" : process.env.NODE_ENV
}`
);
});
}
run()