0.21.3 • Published 8 months ago

@server/next v0.21.3

Weekly downloads
34
License
UNLICENSED
Repository
github
Last release
8 months ago

Server @ Next

⚠️ WIP This is an experimental library right now!

Documentation Here

A fully-fledged web server for Bun and Node.js, with all the basics covered for you:

import server from "@server/next";

// Create a running instance of the server
export default server(options)
  .get("/books", () => Book.list())
  .post("/books", { body: BookSchema }, (ctx) => {
    return Book.create(ctx.body).save();
  });

It includes all the things you would expect from a modern Server framework, like routing, static file serving, body+file parsing, gzip+brotli, streaming, testing, error handling, websockets, etc. We also have integrations with these:

  • KV Stores: in-memory, Redis, Consul, DynamoDB.
  • Buckets: AWS S3, Cloudflare R2, Backblaze B2.
  • Validation libraries: Zod, Joi, Yup.
// Easy testing as well - index.test.js
import app from "./";

it("can retrieve the homepage", async () => {
  const res = await app.fetch(new Request("http://localhost:3000/books/"));
  const books = await res.json();
  expect(books[0]).toEqual({ id: 0, name: 'The Catcher In The Rye', ... });
});

Upgrading server

Why? We live in the era of multi-cloud (Heroku, Workers, Lambda, etc) and multi-runtimes (Node.js, Bun, WinterGC, etc). Desired improvements (WIP!):

  • Tiny footprint with few dependencies. Installing and using the full library takes under 10kb (target limit).
  • Faster! Reimplemented from scratch for speed. With raw ES6+ and a tiny code footprint, your server will fly.
  • Modern ES6+ESM syntax for both the library and examples.
  • Not using express underneath anymore. Considering keeping the compatibility layer anyway (since Express itself is a thin layer).
  • Changed the reply logic greatly, including the removal of render(). This is the main reason express was removed. Many servers don't need render() at all.
  • security Removed mandatory CSRF token, since this is only useful for server-rendered pages and not for SPA. Now we provide an auth module instead.

Progress

  • Router has all verbs, as well as URL pattern matches
  • Full URL parsing, including query and params in ctx.url.
  • Body and Files parsing is working (need testing)
  • The middleware can return:
    • A number and it'll be set as the status code
    • A string and it'll be sent as plain text or html (if it starts with "<")
    • A readStream and it'll be piped to the response
    • An object with status, body and headers and it'll be set raw.
  • Response compression works

Examples

Streams

Creating a 100x100px thumbnail on the fly with Sharp:

// createThumbnail.js
import sharp from "sharp";

export default function createThumbnail(ctx) {
  // Return a pipe, which will be streamed to the output
  return sharp(ctx.url.params.name).resize(100, 100, { fit: "cover" }).png();
}

Breaking Changes

import, export and routing are the main changes from your point of view:

import server, { status, type, ...reply } from 'server';

export default server({ ...options })
  .use(mid1)
  .get('/', cb1)
  .get('/b', mid2, cb2)
  .routes({ get: [['/c', mid3, cb3]] });

status() now it's always partial:

// OLD
return 404;
return status(404).send("Not here..."); // treated as partial
return status(404); // treated as final

// NEW
return 404;
return status(404).send("Not here..."); // GOOD
return status(404).send(); // GOOD

// DON'T DO:
return status(404); // INVALID
import server from "server";

export default server()
  .get("/", () => "Hello world")
  .post("/", (ctx) => {
    console.log(ctx.body);
    return 201;
  });
0.21.3

8 months ago

0.21.2

8 months ago

0.21.1

8 months ago

0.21.0

8 months ago

0.20.50

8 months ago

0.20.48

8 months ago

0.20.49

8 months ago

0.20.46

8 months ago

0.20.47

8 months ago

0.20.45

8 months ago

0.20.44

9 months ago

0.20.42

10 months ago

0.20.43

9 months ago

0.20.40

10 months ago

0.20.41

10 months ago

0.20.39

10 months ago

0.20.37

10 months ago

0.20.38

10 months ago

0.20.35

10 months ago

0.20.36

10 months ago

0.20.33

10 months ago

0.20.34

10 months ago

0.20.31

10 months ago

0.20.32

10 months ago

0.20.30

10 months ago

0.20.28

10 months ago

0.20.29

10 months ago

0.20.26

10 months ago

0.20.27

10 months ago

0.20.24

10 months ago

0.20.25

10 months ago

0.20.22

10 months ago

0.20.23

10 months ago

0.20.20

10 months ago

0.20.21

10 months ago

0.20.19

10 months ago

0.20.17

10 months ago

0.20.18

10 months ago

0.20.15

11 months ago

0.20.16

10 months ago

0.20.13

12 months ago

0.20.14

11 months ago

0.20.11

12 months ago

0.20.12

12 months ago

0.20.10

12 months ago

0.20.9

12 months ago

0.20.8

12 months ago

0.20.7

12 months ago

0.20.6

1 year ago

0.20.5

1 year ago

0.20.4

1 year ago

0.20.1

1 year ago

0.20.3

1 year ago

0.20.2

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.18.0

1 year ago

0.17.2

3 years ago

0.17.3

3 years ago

0.17.1

3 years ago

0.15.0

3 years ago

0.16.0

3 years ago

0.15.1

3 years ago

0.17.0

3 years ago

0.15.2

3 years ago

0.13.0

5 years ago

0.14.0

5 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.6

5 years ago

0.11.3

6 years ago

0.11.4

6 years ago

0.11.5

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.7

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago