Skip to content

Commit

Permalink
Return description from APOD API
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Butz committed Dec 5, 2019
1 parent c4fd546 commit c7bf4c3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# APOD Proxy

Node application that proxies requests to NASA's **A**stronomy **P**icture **O**f the **D**ay API and (soon) caches responses using Redis.

## Setup

An API key for `api.nasa.gov` is required. A key can be generated in about 5 seconds at: https://api.nasa.gov/#signUp

## Run

To run the server on port 3000:

```
NASA_API_KEY=<nasa-api-key-here> npm start
```
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use('/', indexRouter);
app.use('/', indexRouter(process.env.NASA_API_KEY));

module.exports = app;
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"start": "node ./bin/www"
},
"dependencies": {
"axios": "^0.19.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"morgan": "~1.9.1"
},
"engines": {
"node": ">=13.2.0"
}
}
15 changes: 11 additions & 4 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const express = require('express');
const client = require('../services/nasa.client');
const router = express.Router();

router.get('/', function(req, res, next) {
res.json({time: new Date()});
});
function index(nasaApiKey) {
router.get('/', async function(req, res, next) {
const desc = await client.getApodDesc(nasaApiKey)

module.exports = router;
await res.json({ desc });
});

return router;
}

module.exports = index;
16 changes: 16 additions & 0 deletions services/nasa.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const axios = require('axios');

const client = {
getApodDesc: async function(
apiKey,
date = 'today'
) {
const dateQuery = date === 'today' ? '' : `&date=${date}`;
const url = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}${dateQuery}`;
const response = await axios.get(url);

return response.data.explanation;
}
};

module.exports = client;

0 comments on commit c7bf4c3

Please sign in to comment.