-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
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}`) | ||
}) |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
test/* | ||
.DS_Store |