-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
51 lines (40 loc) · 1.15 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express');
const router = express.Router();
// declare axios for making http requests
const axios = require('axios');
const API = 'https://jsonplaceholder.typicode.com';
/* GET api listing. */
router.get('/', (req, res) => {
res.send('api works');
});
// Get all posts
router.get('/stocks', (req, res) => {
// Get posts from the mock api
// This should ideally be replaced with a service that connects to MongoDB
axios.get(`${API}/posts`)
.then(posts => {
res.status(200).json(posts.data);
})
.catch(error => {
res.status(500).send(error)
});
});
router.get('/stock/:symbol', (req, res) => {
// https://api.iextrading.com/1.0
let stocksymbol = req.params["symbol"];
if(stocksymbol != null && stocksymbol != "")
{
axios.get('https://api.iextrading.com/1.0/stock/' + stocksymbol + '/chart/1d')
.then(function (response) {
console.log(response.data);
res.status(200).json(response.data);
})
.catch(function (error) {
console.log(error);
});
res.send(stocksymbol)
} else {
res.send([])
}
})
module.exports = router;