Skip to content

Commit c0554bb

Browse files
committed
fixed proxies and displaying backend data in the front end
1 parent b8ac3d0 commit c0554bb

File tree

9 files changed

+67
-7
lines changed

9 files changed

+67
-7
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
PORT=4001
21
USER=jackfowler
32
DATABASE_URL="postgres://[email protected]:5432/asteroids"
43
NASA_API_KEY=8VuxJOhXZWmsdO97pxA7BgayP0uAWsrK5cFKycIC

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"hack:client": "vue-cli-service serve",
1111
"hack:server": "nodemon server",
1212
"hack": "concurrently \"yarn hack:server\" \"yarn hack:client\"",
13-
"start": "node index.js"
13+
"start": "node server.js"
1414
},
1515
"dependencies": {
1616
"axios": "^0.21.0",

seedFiles/importAsteroids.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const knex = require("knex");
2-
const config = require("./knexfile");
2+
const config = require("../knexfile");
33
const db = knex(config);
44

55
const axios = require('axios');

seedFiles/importItems.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require("dotenv").config();
2-
const config = require("./knexfile");
2+
const config = require("../knexfile");
33
const knex = require("knex");
44
const db = knex(config);
55

seedFiles/seedandMigrate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const knex = require("knex");
2-
const config = require("./knexfile");
2+
const config = require("../knexfile");
33
const db = knex(config);
44

55

server.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ require("dotenv").config();
88

99
const PORT = process.env.PORT || 9000;
1010
// Serve static assets
11+
app.use((req, res, next) => {
12+
res.header('Access-Control-Allow-Origin', '*');
13+
next();
14+
});
1115
app.use(express.static(__dirname, + "/public"));
1216

1317
app.get("/api/asteroids", async (req, res) => {
@@ -21,5 +25,30 @@ app.get("/api/asteroids", async (req, res) => {
2125
}
2226
});
2327

28+
app.get("/api/asteroids/names", async (req, res) => {
29+
try {
30+
const asteroidNames = await db.select("name").table("asteroids");
31+
res.json(asteroidNames);
32+
} catch (err) {
33+
console.error("Error finding names!", err);
34+
res.sendStatus(500);
35+
}
36+
});
37+
38+
app.get("/api/asteroids/:id", async (req, res) => {
39+
const id = req.params.id;
40+
41+
try {
42+
const asteroidNames = await db
43+
.select()
44+
.table("asteroids")
45+
.where({id:id});
46+
res.json(asteroidNames);
47+
} catch (err) {
48+
console.error("Error finding names!", err);
49+
res.sendStatus(500);
50+
}
51+
});
52+
2453
app.listen(PORT, () => console.log(`App listening on port ${PORT}!`));
2554

src/App.vue

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,42 @@
1212
<option>C</option>
1313
</select>
1414
<span>Selected: {{ selected }}</span>
15-
15+
{{asteroidNames}}
1616
<select v-model="selected">
1717
<option disabled value="">Please select one</option>
1818
<option>A</option>
1919
<option>B</option>
2020
<option>C</option>
2121
</select>
2222
<span>Selected: {{ selected }}</span>
23+
24+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
25+
<circle cx="50" cy="50" r="3"/>
26+
</svg>
27+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
28+
<circle cx="50" cy="50" r="3"/>
29+
</svg>
2330
</div>
2431
</template>
2532

2633
<script>
27-
34+
import {asteroidNames} from "../utils";
2835
2936
export default {
3037
name: 'App',
3138
data: () => ({
39+
asteroidNames: [1,2,3],
3240
selected: "",
3341
}),
42+
mounted() {
43+
asteroidNames().then((data) => {
44+
console.log(data)
45+
this.asteroidNames=data;
46+
});
47+
},
48+
methods: {
49+
50+
}
3451
}
3552
</script>
3653

utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import axios from "axios";
2+
3+
export const asteroidNames = async () => {
4+
const { data: names } = await axios.get(`/api/asteroids/names`);
5+
let result = names.map(asteroid => {
6+
return asteroid.name
7+
})
8+
console.log(result)
9+
return result;
10+
};

vue.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
devServer: {
3+
proxy:"http://localhost:9000",
4+
}
5+
}

0 commit comments

Comments
 (0)