Skip to content

Commit

Permalink
refactored code to use response.js functions for http responses
Browse files Browse the repository at this point in the history
  • Loading branch information
marythedev committed Sep 26, 2023
1 parent c25d0b7 commit b7858e9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
9 changes: 2 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const passport = require('passport');
const logger = require('./logger');
const pino = require('pino-http')({ logger });
const authenticate = require('./auth');
const { createErrorResponse } = require('./response');

const app = express();

Expand All @@ -23,13 +24,7 @@ app.use('/', require('./routes'));

//Resources Not Found - 404 middleware
app.use((req, res) => {
res.status(404).json({
status: 'error',
error: {
message: 'Not Found',
code: 404,
},
});
res.status(404).json(createErrorResponse(404, 'Not Found'));
});

//Error-handling middleware to deal with anything else
Expand Down
10 changes: 7 additions & 3 deletions src/routes/api/get.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { createSuccessResponse } = require('../../response');

// Gets a list of fragments for the current user
module.exports = (req, res) => {
// TODO: this is not properly implemented for now (gives a blank list)
res.status(200).json({
status: 'ok',

let data = {
fragments: [],
});
};

res.status(200).json(createSuccessResponse(data));
};
8 changes: 5 additions & 3 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const { version, author } = require('../../package.json');
const { authenticate } = require('../auth');
const { createSuccessResponse } = require('../response');
const router = express.Router();

//Expose all of API routes on /v1/* to include an API version.
Expand All @@ -12,12 +13,13 @@ router.get('/', (req, res) => {
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#controlling_caching
res.setHeader('Cache-Control', 'no-cache');

res.status(200).json({
status: 'ok',
let data = {
author,
githubUrl: 'https://github.com/marythedev/fragments',
version,
});
};

res.status(200).json(createSuccessResponse(data));
});

module.exports = router;

0 comments on commit b7858e9

Please sign in to comment.