Trying to use glitch as an API

Hello, I’m attempting to use glitch as an API for my website since 000webhost doesn’t support node, I attempted to use the fetch function on my glitch site (api.blox.cheap) and got this error.

If anyone could help?

Hi, are you using Express to create your API?

2 Likes

You should be using a server to make an API. As @khalby786 said, Express (the Glitch template default - ~hello-express), is a great option for this use case

Hope this helps :slight_smile:
Eddie

2 Likes

I am, the issue is I can’t send a request from 000webhost to glitch. It doesn’t let me.

Are you getting a 403 error?

Seems like they are getting CORS errors
Sounds like cors package will help!

2 Likes

Are you able to provide me with an example on how to use this?

I attached an image of the error above.

This package isn’t necessary, just add this middleware code to the server.js file

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Put this before all your routing @Pog
Hope this helps! :slight_smile:
Eddie

3 Likes

Yes, that should work!

Disclaimer: allowing cross-origin requests to your API will allow anyone to make requests to your API, so be careful! (But it’s not a bad thing, so don’t worry either :laughing:)

1 Like

Thank you so much for your support, I have managed to do it. :smile:

1 Like

My API won’t be storing any valuable information, it’s just there to allow me to run node.js from 000webhost.