【发布时间】:2017-05-24 12:12:56
【问题描述】:
我使用 Node/Express 作为 API 代理服务器,想知道是否可以为所有 GET 路由呈现 HTML (index.html) 页面以及在同一个端口。我正在使用客户端路由(react-router)。
例如:
// Always return the main index.html, so react-router render the route in the client
app.get('*', (req, res) => {
const html = renderHTML(req, res);
res.send(html)
});
// Expose API
app.all('/api/*', (req, res, next) => {
proxyRequest(...)
});
问题是在做 GET 请求时,第一个 app.get() 捕获它(例如:fetch('/api/accounts'))。
我需要能够向任何 API 端点路由(/ 除外)发出任何请求,并让客户端发送任何方法(GET、PUT、POST...)。
我可以将index.html 提供给所有客户端路由并且在同一端口上有 GET 端点吗?我可以为所有以/api/ 前缀的路由除了提供html吗?
【问题讨论】:
标签: javascript node.js express