Skip to content

Routing

zely.js automatically generates routes based on your file tree of pages.

filenameresult(path)
/index.ts/
/hello.ts/hello
/foo/bar.ts/foo/bar
/user/$id.ts/user/:id
/user/$id/about.ts/user/:id/about

INFO

You can also use nextjs style routes.

Method

Exports functions corresponding get, post, delete etc (http verbs)

ts
// get
export function get(req, res) {}
// post
export function post(req, res) {}
// etc...
// get
export function get(req, res) {}
// post
export function post(req, res) {}
// etc...

Export default experimental

You can write simpler code that outputs the same result since v1.0.

ts
import { ServerDataHandler } from 'zely';
import { GET, POST } from 'zely/methods';

export default [
  // simple usage
  GET({ foo: 'bar' }),
  // access to req and res
  POST((req, res) => ({
    id: req.params.id;
  })),
] as ServerDataHandler[];
import { ServerDataHandler } from 'zely';
import { GET, POST } from 'zely/methods';

export default [
  // simple usage
  GET({ foo: 'bar' }),
  // access to req and res
  POST((req, res) => ({
    id: req.params.id;
  })),
] as ServerDataHandler[];
ts
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.json({ foo: 'bar' });
}

export function post(req: ZelyRequest, res: ZelyResponse) {
  res.json({ id: req.params.id });
}
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.json({ foo: 'bar' });
}

export function post(req: ZelyRequest, res: ZelyResponse) {
  res.json({ id: req.params.id });
}

Route Parameters

You can access the current page parameters by req.params.

ts
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.json({ id: req.params.id });
}
import { ZelyRequest, ZelyResponse } from 'zely';

export function get(req: ZelyRequest, res: ZelyResponse) {
  res.json({ id: req.params.id });
}

Routes using Brackets experimental

If you don't like zely routes style you can use nextjs routes style.

ts
import { defineConfig } from 'zely';

export default defineConfig({
  useBrackets: true,
});
import { defineConfig } from 'zely';

export default defineConfig({
  useBrackets: true,
});

Examples:

filenameresult(path)
/index.ts/
/hello.ts/hello
/foo/bar.ts/foo/bar
/user/[id].ts/user/:id
/user/[id]/about.ts/user/:id/about
/foo/[...bar]/foo/:bar*

Custom Path

If you don't like routes based on filename, just export $page!

ts
export const $page = {
  path: '/foo/bar',
};
export const $page = {
  path: '/foo/bar',
};

404 page

You can set 404 page with config.error.

ts
export default defineConfig({
  error(req, res) {
    // ...
    res.status(404).end('page not found');
  },
});
export default defineConfig({
  error(req, res) {
    // ...
    res.status(404).end('page not found');
  },
});

request and response

Zely Server is based on node:http module.

request

reference: http.req

response

reference: http.res