Skip to content

Commit

Permalink
Using response handlers for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bregwin Jogi committed May 27, 2024
1 parent 2955301 commit 1cf2c8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
25 changes: 11 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const pino = require('pino-http')({
logger,
});

const { createErrorResponse } = require('./response');

// Create an express app instance we can use to attach middleware and HTTP routes
const app = express();

Expand All @@ -38,13 +40,14 @@ app.use('/', require('./routes'));

// Add 404 middleware to handle any requests for resources that can't be found
app.use((req, res) => {
res.status(404).json({
status: 'error',
error: {
message: 'not found',
code: 404,
},
});
// res.status(404).json({
// status: 'error',
// error: {
// message: 'not found',
// code: 404,
// },
// });
res.status(404).json(createErrorResponse(404, 'not found'));
});

// Add error-handling middleware to deal with anything else
Expand All @@ -60,13 +63,7 @@ app.use((err, req, res, next) => {
logger.error({ err }, `Error processing request`);
}

res.status(status).json({
status: 'error',
error: {
message,
code: status,
},
});
res.status(status).json(createErrorResponse(status, message));
});

// Export our `app` so we can access it in server.js
Expand Down
13 changes: 7 additions & 6 deletions src/routes/api/get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// src/routes/api/get.js

const { createSuccessResponse } = require('../../response');
/**
* Get a list of fragments for the current user
*/
module.exports = (req, res) => {
// TODO: this is just a placeholder. To get something working, return an empty array...
res.status(200).json({
status: 'ok',
// TODO: change me
fragments: [],
});

res.status(200).json(
createSuccessResponse({
fragments: [],
})
);
};
18 changes: 10 additions & 8 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const { version, author } = require('../../package.json');
// Create a router that we can use to mount our API
const router = express.Router();

const { createSuccessResponse} = require('../response');
/**
* Expose all of our API routes on /v1/* to include an API version.
*/
router.use(`/v1`,authenticate(), require('./api'));
router.use(`/v1`, authenticate(), require('./api'));

/**
* Define a simple health check route. If the server is running
Expand All @@ -23,13 +24,14 @@ router.get('/', (req, res) => {
// Client's shouldn't cache this response (always request it fresh)
res.setHeader('Cache-Control', 'no-cache');
// Send a 200 'OK' response
res.status(200).json({
status: 'ok',
author,
// Use your own GitHub URL for this!
githubUrl: 'https://github.com/brokoli777/fragments',
version,
});

res.status(200).json(
createSuccessResponse({
author,
githubUrl: 'https://github.com/brokoli777/fragments',
version,
})
);
});

module.exports = router;

0 comments on commit 1cf2c8b

Please sign in to comment.