This is a Next.js project bootstrapped with create-next-app
and
customized to route /api
requests to external middleware -
specifically, the InfluxData iot-app-<js, python, etc.>
example applications.
-
Run the development server:
npm run dev # or yarn dev
-
Open http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying pages/index.tsx
. The page auto-updates as you edit the file.
API routes can be accessed on http://localhost:3000/api/hello. This endpoint can be edited in pages/api/hello.ts
.
To use the UI with one of the iot-api-[language]
example apps, set API_URL
in ./.env.development
to the URL of the API example app.
iot-api-ui
uses a Next.js catch-all route and http-proxy
to proxy all /api
requests to your API middleware server.
See ./pages/api/[...path].js
for the /api
route and proxy configuration.
import httpProxy from 'http-proxy'
/** Set API_URL to the URL of iot-api-js, iot-api-python, etc. **/
const API_URL = process.env.API_URL
const proxy = httpProxy.createProxyServer()
/** Don't parse JSON bodies on the proxied route **/
export const config = {
api: {
bodyParser: false
}
}
export default (req, res) => {
proxy.web(req, res, { target: API_URL, changeOrigin: true })
}
To learn more about Next.js, see following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
- https://maxschmitt.me/posts/next-js-api-proxy/ for the
http-proxy
configuration.