Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wholesoft committed Apr 19, 2023
1 parent 590623f commit 2a7de19
Show file tree
Hide file tree
Showing 18 changed files with 705 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
18 changes: 18 additions & 0 deletions backend/dist/config/corsOptions.js

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

1 change: 1 addition & 0 deletions backend/dist/config/corsOptions.js.map

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

12 changes: 12 additions & 0 deletions backend/dist/index.js

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

2 changes: 1 addition & 1 deletion backend/dist/index.js.map

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

12 changes: 12 additions & 0 deletions backend/dist/middleware/credentials.js

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

1 change: 1 addition & 0 deletions backend/dist/middleware/credentials.js.map

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

139 changes: 139 additions & 0 deletions backend/package-lock.json

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

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^18.15.11",
"typescript": "^5.0.4"
},
"dependencies": {
"axios": "^1.3.5",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
20 changes: 20 additions & 0 deletions backend/src/config/corsOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dotenv from "dotenv"
dotenv.config()

const allowedOrigins = process.env.CORS_ALLOWED_ORIGIN.split(",")

console.log(`Allowed Origins: ${allowedOrigins}`)

const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
console.log(origin)
callback(new Error("Not allowed by CORS"))
}
},
optionsStuccessStatus: 200,
}

export default corsOptions
15 changes: 15 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import express from "express"
import cors from "cors"
import dotenv from "dotenv"
import axios from "axios"
import credentials from "./middleware/credentials.js"
import corsOptions from "./config/corsOptions.js"

const app = express()
app.use(credentials)
app.use(cors(corsOptions))

dotenv.config()

const port = process.env.PORT

app.get("/", (req, res) => {
res.send("API TESTER")
})

app.get("/nasa", async (req, res) => {
const api_key = process.env.NASA_API_KEY
const url = `https://api.nasa.gov/planetary/apod?api_key=${api_key}`
const response = await axios.get(url)
res.send(response.data)
})

app.listen(port, () => {
console.log(`now listening on port ${port}`)
})
15 changes: 15 additions & 0 deletions backend/src/middleware/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dotenv from "dotenv"
dotenv.config()

const allowedOrigins = process.env.CORS_ALLOWED_ORIGIN.split(",")

const credentials = (req, res, next) => {
const origin = req.headers.origin

if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Credentials", true)
}
next()
}

export default credentials
3 changes: 3 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

test/*
.DS_Store
Loading

0 comments on commit 2a7de19

Please sign in to comment.