Hosting an API

So I am new to making web apps and I was trying to make a website with express in backend. I didn't want to spend any money to host it so i tried vercel and after countless errors and asking for help in various server, it turns out u can't host your own api endpoints on your vercel app. So I took some advice from other people and they told me to keep the API on a different server. I remembered that my friend and I share a VPS so I hosted my API on it (Without any domain so i was making requests using the VPS' ip). It worked like a charm on my local machine but I guess vercel doesn't allow outgoing requests to urls without https (SSL certificates) and since my VPS doesnt have a certificate vercel kept requests to it. I asked for help about this and someone told me to try cloudflare and they also mentioned pages and functions. I've never used cloudflare before so I have no idea what to do. Please help
26 Replies
wen
wenOP•3mo ago
help
valkyrie_pilot
valkyrie_pilot•3mo ago
What I would do is purchase a domain, and host your website from your VPS proxied through cloudflare. Potentially using Cloudflare Tunnels, it is a fantastic product
wen
wenOP•3mo ago
hey so im trying to use cf workers to host my api there also using wrangler so like can we make api calls in a worker ??
valkyrie_pilot
valkyrie_pilot•3mo ago
You can make calls to other APIs from a worker. What are you trying to do here?
wen
wenOP•3mo ago
like so
app.get('/geo/:id', (c) => {
let data;
fetch(`https://api.gep/?id=${c.req.param('id')}`).then(async (result) =>
console.log('call')
);
console.log(data);
return c.text(`test`);
});
app.get('/geo/:id', (c) => {
let data;
fetch(`https://api.gep/?id=${c.req.param('id')}`).then(async (result) =>
console.log('call')
);
console.log(data);
return c.text(`test`);
});
but its logging undefined also call isnt being logged what u think the problem is ??
valkyrie_pilot
valkyrie_pilot•3mo ago
then is not async You should do some research on async in javascript. However... cloudflare workers don't use express, so...
wen
wenOP•3mo ago
nah cuz in my actual code i need to do result.json() which returns a promise
wen
wenOP•3mo ago
No description
wen
wenOP•3mo ago
im using hono
valkyrie_pilot
valkyrie_pilot•3mo ago
can you should your actual code?
wen
wenOP•3mo ago
app.get('/geo/:id', (c) => {
let data;
fetch(`https://api.ip2location.io/?key=[key]&ip=${c.req.param('id')}`).then(
async (result) => (data = await result.json())
);
console.log(data);
return c.text(`test`);
});
app.get('/geo/:id', (c) => {
let data;
fetch(`https://api.ip2location.io/?key=[key]&ip=${c.req.param('id')}`).then(
async (result) => (data = await result.json())
);
console.log(data);
return c.text(`test`);
});
valkyrie_pilot
valkyrie_pilot•3mo ago
Oop redact your API key!
wen
wenOP•3mo ago
yup sorry
valkyrie_pilot
valkyrie_pilot•3mo ago
Cloudflare will do a little bit of geolocation for you there, btw, in the request.cf object.
wen
wenOP•3mo ago
idk what that is its my 1st time
valkyrie_pilot
valkyrie_pilot•3mo ago
if you want to know where an IP is located, cloudflare provides a little data on that
wen
wenOP•3mo ago
how do i access the said data
valkyrie_pilot
valkyrie_pilot•3mo ago
You probably need something more like this.
app.get('/geo/:id', async (c) => {
let request = await fetch(`https://api.ip2location.io/?key=[key]&ip=${c.req.param('id')}`);
let data = await request.json();
console.log(data);
return c.text(`test`);
});
app.get('/geo/:id', async (c) => {
let request = await fetch(`https://api.ip2location.io/?key=[key]&ip=${c.req.param('id')}`);
let data = await request.json();
console.log(data);
return c.text(`test`);
});
Hono seems to support async handlers, so you need your whole handler to be async. That way you can actually wait for there to be data there before you try to print it. With hono, I am not sure. I only know with the basic Workers JS SDK.
wen
wenOP•3mo ago
yooo it worked this tysm 🫂
valkyrie_pilot
valkyrie_pilot•3mo ago
yw! async is very tricky, heh enjoy learning to code!
wen
wenOP•3mo ago
thanks !!
gwapes
gwapes•3mo ago
GitHub
How do I access cf object on the context? · honojs · Discussion #961
I have just started a new project with hono 3.0.2 using create-hono CLI. I could previously access the cloudflare object and its properties in workers project with Hono but they seem to be not avai...
valkyrie_pilot
valkyrie_pilot•3mo ago
yep
wen
wenOP•3mo ago
got a problem @gwapes im using wrangler and trying to use namespaces and docs show to use env.[namespace_name].put(...) but i get ReferenceError: env is not defined
valkyrie_pilot
valkyrie_pilot•3mo ago
i don’t know on that one
wen
wenOP•3mo ago
np
Want results from more Discord servers?
Add your server