Skip to content

Commit

Permalink
Add 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
emagnier committed Feb 8, 2020
1 parent 22b8839 commit d9f3eea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Note: this step as well as tests are run automatically before releasing a new ve

## History

- 1.0.4 Add 404 error message on "Not found pages".
- 1.0.3 Get romcal-api version from the API. Support optional parameters when using romcal-api through an Express middleware.
- 1.0.2 Readme fine-tuning.
- 1.0.1 New logo for romcal-api.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "romcal-api",
"version": "1.0.3",
"version": "1.0.4",
"description": "REST API for liturgical calendars in Catholic Roman rite (Western Church). Powered by romcal.",
"main": "dist/src/index.js",
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import defaultConfig from '../config';
import Calendar from './calendar/calendar';
import Calendars from './calendars/calendars';
import Locale from './locale/locale';
import PageNotFound from './page-not-found/page-not-found';
import Version from './version/version';

const router = express.Router();
Expand All @@ -16,6 +17,7 @@ function initRouter(config?: {baseUrl: string}) {
router.get(`${c.baseUrl}/calendars`, Calendars.getAllCalendars);
router.get(`${c.baseUrl}/locales`, Locale.getAllLocales);
router.get(`${c.baseUrl}/version`, Version.getVersion);
router.get(`${c.baseUrl}/*`, PageNotFound.get404Content);

return router;
}
Expand Down
18 changes: 18 additions & 0 deletions src/routes/page-not-found/page-not-found.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import request from 'supertest';
import app from '../../app';

describe( `GET /*`, () => {
it('The API root returns a 404 error', async () => request(app())
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(404)
);

it('Any URL not member of the API endpoints returns a 404 error', async () => request(app())
.get('/lorem-ipsum')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(404)
);
});
8 changes: 8 additions & 0 deletions src/routes/page-not-found/page-not-found.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default class PageNotFound {
static get404Content(_req, res) {
return res.status(404).send({
error: '404',
message: 'Page not found.',
});
}
}

0 comments on commit d9f3eea

Please sign in to comment.