Skip to content

Commit ec9f0be

Browse files
joybrobongsok
authored andcommitted
Make articleId include slash(/)
This wiki supports hierarchy b/w articles. For example, an article 'ab/cd' is under the 'ab' article conceptually. For this purpose, article id need to be able to include slash. References: - remix-run/react-router#391 (comment) - https://stackoverflow.com/questions/10020099/express-js-routing-optional-spat-param/14481168#comment49246489_29549148
1 parent a25f76e commit ec9f0be

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

server/app.jsx

+8-6
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,27 @@ app.use(session.createSessionMiddleware());
5454
app.use(express.static(path.join(__dirname, './public')));
5555

5656
// Article APIs
57-
app.post('/api/article/:id', (req, res) => {
57+
app.post('/api/article/*?', (req, res) => {
5858
const func = 'app.post /api/article/:id';
5959
console.log({ file, func, params: req.params, body: req.body});
6060

6161
const { content } = req.body;
6262

63-
article.create(req.params.id, content)
63+
article.create(req.params[0], content)
6464
.then(result => res.send(result))
6565
.catch(error => {
6666
console.log({ file, func, error });
6767
res.status(403).send(error);
6868
});
6969
});
7070

71-
app.get('/api/article/:id', (req, res) => {
71+
app.get('/api/article/*?', (req, res) => {
7272
const func = 'app.get /api/article/:id';
7373
console.log({ file, func, params: req.params });
7474

75-
article.read(req.params.id)
75+
console.log(req.params);
76+
77+
article.read(req.params[0])
7678
.then(result => {
7779
console.log({ file, func, result });
7880
res.send(result)
@@ -83,13 +85,13 @@ app.get('/api/article/:id', (req, res) => {
8385
});
8486
});
8587

86-
app.put('/api/article/:id', (req, res) => {
88+
app.put('/api/article/*?', (req, res) => {
8789
const func = 'app.put /api/article/:id';
8890
console.log({ file, func, params: req.params, body: req.body });
8991

9092
const { content, rev } = req.body;
9193

92-
article.update(req.params.id, content, rev)
94+
article.update(req.params[0], content, rev)
9395
.then(result => res.send(result))
9496
.catch(error => {
9597
console.log({ file, func, error });

src/containers/ArticleContainer.jsx

+4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import connectWithRouter from '../../modules/connectWithRouter';
77
import Article from '../components/Article';
88
import { updateArticle } from '../actions/article';
99

10+
const file = '/src/containers/ArticleContainer';
11+
1012
const ArticleContainer = (props) => {
1113
const { pageId } = props.match.params;
1214
const { content, revision, onReadArticle } = props;
1315

16+
console.log({ file, func: 'ArticleContainer', pageId });
17+
1418
onReadArticle(pageId);
1519

1620
return (<Article pageId={pageId} content={content} revision={revision}/>);

src/containers/MainContainer.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const MainContainer = () => (
2020
<Route path='/hello' component={Hello}/>
2121
<Route path='/about' component={About}/>
2222
<Route exact path='/w' component={ArticleContainer}/>
23-
<Route exact path='/w/:pageId' component={ArticleContainer}/>
24-
<Route exact path='/edit/:pageId' component={EditContainer}/>
23+
<Route exact path='/w/:pageId(.+)' component={ArticleContainer}/>
24+
<Route exact path='/edit/:pageId(.+)' component={EditContainer}/>
2525
</Switch>
2626
</Col>
2727
</Row>

0 commit comments

Comments
 (0)