私はSSRの反応に問題があります。私は私のアプリルートマネージャーとして反応ルータを使用します。RESTful API(react-router + express)を使ってサーバー側レンダリングを行う方法はありますか?
サーバ側で、それはこの(server.js)のように見えることがあります。
var app = express();
app.get("*",function(req, res){
match({routes:AppRoutes, location:req.url},
function(err, redirectLocation, renderProps){
if (error) {
res.send(500, error.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
res.send(200, renderToString(<RoutingContext {...renderProps} />))
} else {
res.send(404, 'Not found')
}
});
});
をする場合には反応し、ルータはすべてのGET
ルートを消費し、私は、サーバー側でのRESTfulなAPIを必要とする場合、どのように行いますそれを反応経路から分離するか?
そして、私はフロントエンドの成分を持つ場合:
class Post extends Component{
componentDidMount(){
fetch('/api/post')
.then(function(response){
//change component state or do other
})
.catch(function(err){
//handle error
})
}
render(){
//
}
}
は、どのようにこのコンポーネントは、RESTfulなAPIにより、サーバと通信していますか?
Expressは、そのようなRESTfulなAPI構造を提供できますか?
app.get("/api/post",function(){
//do something and response
});
私は本当に理解していません。
私の質問を解決してくれてありがとう!私はこれらの日に反応ルータを学び、サーバー側のレンダリングについて混乱しました。私はRegexを使ってルートにマッチさせようと試みました(恐ろしい方法です!)。私はちょうど今この答えを試して、それは動作します! – Betamee