确实有一百万种方法可以做到这一点,我认为更完整的答案需要关于“你想如何完成这个”的更完整的细节。
您需要研究的是“路径变量”。
在 Express 中,使用路径变量的路由将如下所示:
app.get('/articles/:article_id', (request, result) => {
const {article_id} = request.params;
//... Make database query using path variable
});
//Using two path variables
app.get('/articles/:article_id/:some_other', (request, result) => {
const {article_id, some_other} = request.params;
//... Make database query using path variables
});
在 React 中,你会想要使用 React Router 并制作一个 Route 和一些组件,如下所示:
//... surrounding parent component
<Route path='articles/:article_id'>
<ArticlePage />
</Route>
//... surrounding parent component
文章页面:
import React from 'react';
import { useParams } from 'react-router-dom';
function ArticlePage() {
const params = useParams();
//... Make API call with params.article_id
//... Render component with data from API call
}
您还需要担心“分页”或“无限滚动”来加载文章以供显示(在用户选择之前)。另一个有很多不同解决方案的问题,但我建议为这类事情寻找一个 npm 包。