Skip to content

Commit

Permalink
create middleware to catch non-existent routes
Browse files Browse the repository at this point in the history
[Finishes #164694145]
  • Loading branch information
Andela authored and Andela committed Mar 19, 2019
1 parent 70cdda1 commit e01cf4e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
7 changes: 6 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ app.use(expressSession({
saveUninitialized: true
}));

// Setup a default catch-all route that sends back a welcome message in JSON format.
// Setup an index route
app.get('/', (req, res) => res.status(200).send({
message: 'Authors Haven.'
}));

// Routes
app.use('/api', routes);

// Return 404 for nonexistent routes
app.use((req, res) => {
res.status(404).send({ message: 'Route not found' });
});

// Set Port
const port = process.env.PORT || 3000;

Expand Down
4 changes: 2 additions & 2 deletions server/middlewares/ValidateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { body, validationResult } from 'express-validator/check';

const highltght = [
const highlight = [
body('highlighted')
.exists()
.withMessage('highlighted required')
Expand Down Expand Up @@ -33,4 +33,4 @@ const validationHandler = (req, res, next) => {
return next();
};

export { highltght, validationHandler };
export { highlight, validationHandler };
2 changes: 1 addition & 1 deletion server/routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ router.post('/articles/:slug/comment',
router.post('/articles/:slug/highlight',
AuthenticateUser.verifyUser,
AuthenticateArticle.verifyArticle,
Validate.highltght,
Validate.highlight,
Validate.validationHandler,
Comment.highlight);

Expand Down
13 changes: 13 additions & 0 deletions server/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,16 @@ describe('Toggle notification', () => {
});
});
});

describe('Test wild card route', () => {
it('Should return an 404 error for unfound routes', (done) => {
chai
.request(app)
.get('/api/reportadmin')
.end((req, res) => {
expect(res.status).to.equal(404);
expect(res.body.message).to.be.a('string').to.equal('Route not found');
done();
});
});
});

0 comments on commit e01cf4e

Please sign in to comment.