Added CORS proxy serverless

This commit is contained in:
Galax 2021-11-15 09:36:13 +07:00
parent 6b21925bf2
commit a8faeb2d83
5 changed files with 25 additions and 8 deletions

16
api/index.js Normal file
View File

@ -0,0 +1,16 @@
const app = require("express")();
const axios = require("axios").default;
const cors = require("cors");
app.use(cors());
app.get("/api/:query", async (req, res) => {
res.setHeader("Content-Type", "text/html");
res.setHeader("Cache-Control", "s-max-age=1, stale-while-revalidate");
const googleReq = await axios.get(
`https://suggestqueries.google.com/complete/search?client=chrome&q=${req.params.query}`
);
res.json(googleReq.data);
});
module.exports = app;

View File

@ -8,8 +8,11 @@
"@types/react-autosuggest": "^10.1.5",
"@types/react-dom": "^17.0.10",
"animejs": "^3.2.1",
"axios": "^0.24.0",
"bowser": "^2.11.0",
"boxicons": "^2.0.9",
"cors": "^2.8.5",
"express": "^4.17.1",
"react": "^17.0.2",
"react-autosuggest": "^10.1.0",
"react-dom": "^17.0.2",

View File

@ -3,11 +3,11 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="A Customizable Startpage" />
<meta name="description" content="A Customizable Startpage." />
<title>New Tab</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
</html>

View File

@ -17,12 +17,7 @@ class Search extends Component {
};
onSuggestionsFetchRequested = ({ value }) => {
fetch(
`https://thingproxy.freeboard.io/fetch/https://suggestqueries.google.com/complete/search?client=chrome&q=${value.replace(
" ",
"_"
)}`
)
fetch(`/api/${value.replace(" ", "_")}`)
.then((res) => res.json())
.then((data) => {
const newSuggestions: string[] = data[1];

3
vercel.json Normal file
View File

@ -0,0 +1,3 @@
{
"rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}