NextJS 应用程序和 express 服务器之间的内部 API 调用和通信
Internal API calls and communication between NextJS app and express server
我有一个NextJS app with a custom express server。我的 pages/index.js
正在对我的快速路线 /api/users
进行内部呼叫。这是在 getinitialprops()
中使用 isomorphic-unfetch
完成的,如下所示:
const res = await fetch('http://localhost:3000/api/users');
我在 NextJS 之上的自定义 Express 服务器有 API 发送回 JSON 数据的路由。像这样:
//some code before
const app = next({ dev })
const handle = app.getRequestHandler()
let apiRoutes = require('./routes/apiRoutes.js');
app.prepare().then(() => {
const server = express()
server.use('/api', apiRoutes);
server.get('*', (req, res) => {
return handle(req, res)
})
//more code
所以我的问题是,这是我的客户端代码和服务器端代码之间进行通信的方式吗?如果是这样:
- 我如何保护这些端点,以便用户不只是键入 myNextJSwebsite。com/api/users 并获得 JSON 响应?
我有一个NextJS app with a custom express server。我的 pages/index.js
正在对我的快速路线 /api/users
进行内部呼叫。这是在 getinitialprops()
中使用 isomorphic-unfetch
完成的,如下所示:
const res = await fetch('http://localhost:3000/api/users');
我在 NextJS 之上的自定义 Express 服务器有 API 发送回 JSON 数据的路由。像这样:
//some code before
const app = next({ dev })
const handle = app.getRequestHandler()
let apiRoutes = require('./routes/apiRoutes.js');
app.prepare().then(() => {
const server = express()
server.use('/api', apiRoutes);
server.get('*', (req, res) => {
return handle(req, res)
})
//more code
所以我的问题是,这是我的客户端代码和服务器端代码之间进行通信的方式吗?如果是这样:
- 我如何保护这些端点,以便用户不只是键入 myNextJSwebsite。com/api/users 并获得 JSON 响应?