Skip to content

Commit

Permalink
improving documentation around servers
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Apr 17, 2024
1 parent e22dabf commit 6758079
Show file tree
Hide file tree
Showing 170 changed files with 1,073 additions and 714 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- if: contains(github.head_ref, 'release--')
name: Install dfx
run: |
DFXVM_INIT_YES=true DFX_VERSION=0.18.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
DFXVM_INIT_YES=true DFX_VERSION=0.19.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
echo "$HOME/.local/share/dfx/bin" >> $GITHUB_PATH
# TODO we should use some Action-specific bot account
- if: contains(github.head_ref, 'release--')
Expand Down Expand Up @@ -241,7 +241,7 @@ jobs:
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
name: Install dfx
run: |
DFXVM_INIT_YES=true DFX_VERSION=0.18.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
DFXVM_INIT_YES=true DFX_VERSION=0.19.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
echo "$HOME/.local/share/dfx/bin" >> $GITHUB_PATH
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/azle.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/candid.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/candid_based_documentation.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/canister_lifecycle.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/canisters_overview.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/caveats.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/cross_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/cycles.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/deployment.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/deployment_candid_based.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/examples.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/get_started.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/hello_world.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/http.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/installation.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/internet_computer_overview.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/management_canister.html

Large diffs are not rendered by default.

201 changes: 143 additions & 58 deletions docs/print.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/query_methods.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/bitcoin.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/accept_message.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/arg_data_raw.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/arg_data_raw_size.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call_apis.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call_raw.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call_raw128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call_with_payment.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/call_with_payment128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/caller.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/method_name.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_accept.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_accept128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_available.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_available128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_refunded.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/msg_cycles_refunded128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/notify.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/notify_raw.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/notify_with_payment_128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/reject.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/reject_code.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/reject_message.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/reply.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/call_apis/reply_raw.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/blob.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/bool.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/candid.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/empty.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/float32.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/float64.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/func.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/int.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/int16.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/int32.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/int64.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/int8.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/nat.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/nat16.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/nat32.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/nat64.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/nat8.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/null.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/opt.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/principal.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/record.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/reserved.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/service.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/text.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/variant.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/candid/vec.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/candid_decode.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/candid_encode.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/canister_apis.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/canister_balance.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/canister_balance128.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/canister_id.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/canister_version.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/data_certificate.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/instruction_counter.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/is_controller.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/performance_counter.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/print.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/set_certified_data.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/time.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_apis/trap.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/canister_methods.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/heartbeat.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/http_request.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/http_request_update.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/init.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/inspect_message.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/post_upgrade.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/pre_upgrade.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/query.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/canister_methods/update.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/environment_variables.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/bitcoin_get_utxos.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/canister_status.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/create_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/delete_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/deposit_cycles.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/ecdsa_public_key.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/http_request.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/install_code.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/raw_rand.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/sign_with_ecdsa.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/start_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/stop_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/uninstall_code.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/management_canister/update_settings.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/plugins.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/reference.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable64_grow.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable64_read.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable64_size.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable64_write.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_bytes.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_grow.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_memory.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_read.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_size.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_structures.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/stable_memory/stable_write.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/timers/clear_timer.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/timers/set_timer.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/timers/set_timer_interval.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/timers/timers.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference/wasm_binary_optimization.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/reference_http/autoreload.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/reference_http/reference.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/rest_based_examples.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/searchindex.json

Large diffs are not rendered by default.

303 changes: 0 additions & 303 deletions docs/server.html

This file was deleted.

388 changes: 388 additions & 0 deletions docs/servers.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/stable_structures.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/the_azle_book.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/timers.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/update_methods.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/hello_world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ node --version
Install the dfx command line tools for managing ICP applications:

```bash
DFX_VERSION=0.18.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
DFX_VERSION=0.19.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
```

Check that the installation went smoothly by looking for clean output from the following command:
Expand Down
2 changes: 1 addition & 1 deletion the_azle_book/book/404.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/azle.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/candid.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/canisters_overview.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/cross_canister.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/cycles.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions the_azle_book/book/deployment.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/examples.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/http.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/installation.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/internet_computer_overview.html

Large diffs are not rendered by default.

201 changes: 143 additions & 58 deletions the_azle_book/book/print.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/searchindex.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/book/timers.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion the_azle_book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [Get Started](./get_started.md)
- [Examples](./rest_based_examples.md)
- [Deployment](./deployment.md)
- [Server](./server.md)
- [Servers](./servers.md)
- [Assets]()
- [Debugging]()
- [Limitations]()
Expand Down
2 changes: 1 addition & 1 deletion the_azle_book/src/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ node --version
Install the dfx command line tools for managing ICP applications:

```bash
DFX_VERSION=0.16.1 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
DFX_VERSION=0.19.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
```

Check that the installation went smoothly by looking for clean output from the following command:
Expand Down
123 changes: 0 additions & 123 deletions the_azle_book/src/server.md

This file was deleted.

227 changes: 227 additions & 0 deletions the_azle_book/src/servers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
# Servers TLDR

Just write Node.js servers like this:

```typescript
import { createServer } from 'http';

const server = createServer((req, res) => {
res.write('Hello World!');
res.end();
});

server.listen();
```

or write Express servers like this:

```typescript
import express, { Request } from 'express';

let db = {
hello: ''
};

const app = express();

app.use(express.json());

app.get('/db', (req, res) => {
res.json(db);
});

app.post('/db/update', (req: Request<any, any, typeof db>, res) => {
db = req.body;

res.json(db);
});

app.use(express.static('/dist'));

app.listen();
```

# Servers

- [Node.js http.server](#nodejs-httpserver)
- [Express](#express)
- [Server](#server)
- [Limitations](#limitations)

Azle supports building HTTP servers on ICP using the [Node.js http.Server](https://nodejs.org/api/http.html#class-httpserver) class as the foundation. These servers can serve static files or act as API backends, or both.

Azle currently has good but not comprehensive support for [Node.js http.Server](https://nodejs.org/api/http.html#class-httpserver) and [Express](https://expressjs.com/). Support for other libraries like [Nest](https://nestjs.com/) are works-in-progress.

Once [deployed](./deployment.md) you can access your server at a URL like this locally `http://bkyz2-fmaaa-aaaaa-qaaaq-cai.localhost:8000` or like this on mainnet `https://bkyz2-fmaaa-aaaaa-qaaaq-cai.raw.icp0.io`.

You can use any HTTP client to interact with your server, such as `curl`, `fetch`, or a web browser. See the [Interacting with your canister section](./deployment.md#interacting-with-your-canister) of the [deployment chapter](./deployment.md) for help in constructing your canister URL.

## Node.js http.server

Azle supports instances of [Node.js http.Server](https://nodejs.org/api/http.html#class-httpserver). `listen()` must be called on the server instance for Azle to use it to handle HTTP requests. Azle does not respect a port being passed into `listen()`. The port is set by the ICP replica (e.g. `dfx start --host 127.0.0.1:8000`), not by Azle.

Here's an example of a very simple [Node.js http.Server](https://nodejs.org/api/http.html#class-httpserver):

```typescript
import { createServer } from 'http';

const server = createServer((req, res) => {
res.write('Hello World!');
res.end();
});

server.listen();
```

## Express

[Express](https://expressjs.com/) is one of the most popular backend JavaScript web frameworks, and it's the recommended way to get started building servers in Azle. Here's the main code from the [hello_world example](https://github.com/demergent-labs/azle/tree/main/examples/hello_world):

```typescript
import express, { Request } from 'express';

let db = {
hello: ''
};

const app = express();

app.use(express.json());

app.get('/db', (req, res) => {
res.json(db);
});

app.post('/db/update', (req: Request<any, any, typeof db>, res) => {
db = req.body;

res.json(db);
});

app.use(express.static('/dist'));

app.listen();
```

### jsonStringify

When working with `res.json` you may run into errors because of attempting to send back JavaScript objects that are not strictly `JSON`. This can happen when trying to send back an object with a `BigInt` for example.

Azle has created a special function called `jsonStringify` that will serialize many ICP-specific data structures to `JSON` for you:

```typescript
import { jsonStringify } from 'azle';
import express, { Request } from 'express';

let db = {
bigInt: 0n
};

const app = express();

app.use(express.json());

app.get('/db', (req, res) => {
res.send(jsonStringify(db));
});

app.post('/db/update', (req: Request<any, any, typeof db>, res) => {
db = req.body;

res.send(jsonStringify(db));
});

app.use(express.static('/dist'));

app.listen();
```

## Server

If you need to add [canister methods](./query_methods.md) to your HTTP server, the `Server` function imported from `azle` allows you to do so.

Here's an example of a very simple HTTP server:

```typescript
import { Server } from 'azle';
import express from 'express';

export default Server(() => {
const app = express();

app.get('/http-query', (_req, res) => {
res.send('http-query-server');
});

app.post('/http-update', (_req, res) => {
res.send('http-update-server');
});

return app.listen();
});
```

You can add canister methods like this:

```typescript
import { query, Server, text, update } from 'azle';
import express from 'express';

export default Server(
() => {
const app = express();

app.get('/http-query', (_req, res) => {
res.send('http-query-server');
});

app.post('/http-update', (_req, res) => {
res.send('http-update-server');
});

return app.listen();
},
{
candidQuery: query([], text, () => {
return 'candidQueryServer';
}),
candidUpdate: update([], text, () => {
return 'candidUpdateServer';
})
}
);
```

The `default` export of your `main` module must be the result of calling `Server`, and the callback argument to `Server` must return a [Node.js http.Server](https://nodejs.org/api/http.html#class-httpserver). The `main` module is specified by the `main` property of your project's [dfx.json file](https://github.com/demergent-labs/azle/blob/main/examples/hello_world/dfx.json#L5). The `dfx.json` file must be at the root directory of your project.

The callback argument to `Server` can be asynchronous:

```typescript
import { Server } from 'azle';
import { createServer } from 'http';

export default Server(async () => {
const message = await asynchronousHelloWorld();

return createServer((req, res) => {
res.write(message);
res.end();
});
});

async function asynchronousHelloWorld() {
// do some asynchronous task
return 'Hello World Asynchronous!';
}
```

## Limitations

For a deeper understanding of possible limitations you may want to refer to [The HTTP Gateway Protocol Specification](https://internetcomputer.org/docs/current/references/http-gateway-protocol-spec).

- The top-level route `/api` is currently reserved by the replica locally
- The `Transfer-Encoding` header is not supported
- `gzip` responses most likely do not work
- HTTP requests are generally limited to ~2 MiB
- HTTP responses are generally limited to ~3 MiB
- You cannot set HTTP status codes in the 1xx range

0 comments on commit 6758079

Please sign in to comment.