【问题标题】:Serving SPA from API server从 API 服务器提供 SPA
【发布时间】: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


    【解决方案1】:

    添加一个好主意是将 api 前缀添加到您的其余 API 端点。为您的用户 api 点赞 /api/users

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2018-06-25
      • 2011-11-11
      • 2023-03-25
      • 2017-12-20
      相关资源
      最近更新 更多