0

据我所知,服务SPA的事实上的方法是通过实现GET请求的所有方法。那么,如果我既托管API并从同一服务器提供SPA,又需要检索资源/users从API服务器服务SPA

如何处理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}`) 
}) 

直觉告诉我,所有的实施渔获量为SPA GET请求,同时实现一个例外的API调用/api资源。但是,通过浏览器访问/api存在问题。

任何想法非常感谢。

回答

0

添加好主意会为您的其余API端点添加api前缀。像/api/users为您的用户api。