cs641311
cs641311

Reputation: 100

How do I store server-side data on github?

I am using Github pages to build a website, but I can't figure out how to store data on the server-side. I can use localStorage and sessionStorage, but those are client-side. Does anyone have a way to use javascript or another programming language that works on Github to store data? Or can static websites not store data?

I have already googled "server-side Github" and "javascript server-side" and researched other programming languages like PHP(doesn't work on Github)

Upvotes: 2

Views: 3133

Answers (2)

amir
amir

Reputation: 116

Static websites would need to communicate with a server through an API to store data. You could run that server yourself or use a third-party service to do it. If you're just starting out and prototyping a new app, it makes sense to leverage existing services as much as possible to validate your product.

If you've been using localStorage on the client-side and want a similar API where the data is stored on a server, I recommend looking into a remote key-value store like KVdb.io (disclaimer: I built it, but it's free to use!).

For example:

<script src="https://unpkg.com/[email protected]"></script>
<script>
const kvdbStorage = KVdb.bucket('MY_BUCKET_ID').localStorage()

kvdbStorage.setItem('my-key', 'my-value')
  .then(() => console.log('key saved')
  .then(() => kvdbStorage.getItem('my-key'))
  .then(value => console.log('get value', value))
  .catch(err => console.error(err)
</script>

If your data can be modeled as key-value pairs, this approach can get you pretty far without having to manage a database yourself.

Upvotes: 2

bk2204
bk2204

Reputation: 76944

Static websites, such as those created with GitHub Pages, are just static assets; that is, they are HTML, JavaScript, and CSS without any backend (server-side components) whatever beyond a basic web server. The advantage to this is that they can run anywhere on any web server, but as you've noticed, since they have no backend components, they're somewhat limited.

GitHub Pages is intended to be used for hosting a website for your open source project, so it doesn't provide backend hosting. If you want that, you'll need to investigate alternatives.

Upvotes: 1

Related Questions