【发布时间】:2017-06-08 14:44:01
【问题描述】:
据我了解,提供 SPA 服务的实际方法是为 GET 请求实现 catch all 方法。那么,如果我同时托管 API 并从同一服务器提供 SPA,并且需要检索资源,例如/users?
在您的 API 服务器为您的 SPA 服务的设置中,您如何处理路由 API 调用/资源?
例如使用快递:
import express from 'express'
import path from 'path'
const app = express()
const port = 3000
app.use(express.static(path.resolve(__dirname, '../client/dist')));
// Conflict here <---------------------
app.get('/users', (req,res) => {
// respond with users
})
// And here <---------------------
app.all('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client/dist', 'index.html'));
})
app.listen(process.env.PORT || port, () => {
console.log(`App listening on ${process.env.PORT || port}`)
})
直觉告诉我在为 API 调用实现 /api 资源的异常时,为 SPA 的 GET 请求实现全部捕获。但是通过浏览器访问/api 就会出现问题。
非常感谢任何想法。
【问题讨论】:
标签: javascript rest architecture client-server single-page-application