Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Make routes more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Nov 8, 2016
1 parent b698cd7 commit d0a2f07
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions server/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,47 +99,17 @@ app.get("/new", (req, res, next) => {
.catch(next);
});

app.get("/:id.json", (req, res, next) => {
const id = req.params.id;

let cursor = req.query.cursor;
if (Array.isArray(cursor)) {
return res.sendStatus(400);
} else if (cursor !== undefined) {
cursor = Number(cursor);
}

store.get(id)
.then(item => {
if (!item || !item.isView) {
return res.sendStatus(404);
}

return store.list(item.other, cursor).then(({ cursor, visits }) => {
res.json({
cursor: cursor,
trapUrl: fullUrl(req, item.other),
visits: visits.map(entity => {
return mergeAndClean(entity.info, {
timestamp: entity.timestamp
});
})
});
});
})
.catch(next);
});

app.get("/:id", (req, res, next) => {
analytics.pageView(req).catch(errors.report);

app.get("/:id([a-zA-Z0-9_-]{22})", (req, res, next) => {
const id = req.params.id;
store.get(id)
.then(item => {
if (!item) {
return res.sendStatus(404);
next();
return;
}

analytics.pageView(req).catch(errors.report);

if (!item.isView) {
analytics.event(req, "trap", "view").catch(errors.report);

Expand Down Expand Up @@ -184,6 +154,39 @@ app.get("/:id", (req, res, next) => {
.catch(next);
});

app.get("/:id([a-zA-Z0-9_-]{22}).json", (req, res, next) => {
const id = req.params.id;

let cursor = req.query.cursor;
if (Array.isArray(cursor)) {
return res.sendStatus(400);
}
if (cursor !== undefined) {
cursor = Number(cursor);
}

store.get(id)
.then(item => {
if (!item || !item.isView) {
next();
return;
}

return store.list(item.other, cursor).then(({ cursor, visits }) => {
res.json({
cursor: cursor,
trapUrl: fullUrl(req, item.other),
visits: visits.map(entity => {
return mergeAndClean(entity.info, {
timestamp: entity.timestamp
});
})
});
});
})
.catch(next);
});

app.use(errors.express);

const server = app.listen(process.env.PORT || 8080, () => {
Expand Down

0 comments on commit d0a2f07

Please sign in to comment.