ChatiumFor developersPlaygroundPricing
Sign in

Writing Server Code

Server code in Chatium is written in TypeScript. It is most similar to Node.js frameworks like Express.

Let's try adding code that simply returns data in JSON format.

The simplest example of a file:

app.get('/', () => {
  return {
    hello: "World"
  }
})

Now, let's add this code to our account. To do this:

  • Go to the IDE and click the "Create File" button.
  • Select "Server TypeScript," enter the file name, for example, sample.tsx you can also omit the .tsx extension; it will be added automatically.
  • In the created file, enter the above code and click "Save."
  • To see the result, we need to open the file in a browser. For this, open the address:
https://<account.domain>/sample

In GetCourse, the address will be slightly different:

https://<account.domain>/chtm/sample

And you will see the result in JSON format:

{
  hello: "World"
}

Now let's try to return something more meaningful, for example, the current user's data:

We will replace the code with the following:

app.get('/', () => {
  return {
    user: ctx.user,
    isAdmin: ctx.user?.is('Admin'),
  }
})

If we refresh the page in the browser, we will see a JSON response that returns data about the current user.

Let's try opening it in private mode, and we will see that the response is different (it will be an empty JSON because it will be an unauthorized user).