0.25.6 • Published 9 months ago

@server/next v0.25.6

Weekly downloads
34
License
UNLICENSED
Repository
github
Last release
9 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.25.4

11 months ago

0.21.8

1 year ago

0.25.3

11 months ago

0.21.7

1 year ago

0.25.2

11 months ago

0.21.6

1 year ago

0.25.1

12 months ago

0.21.5

1 year ago

0.25.0

12 months ago

0.21.4

1 year ago

0.25.6

9 months ago

0.25.5

11 months ago

0.21.9

1 year ago

0.22.1

1 year ago

0.22.0

1 year ago

0.21.14

1 year ago

0.21.12

1 year ago

0.21.13

1 year ago

0.21.10

1 year ago

0.21.11

1 year ago

0.23.1

1 year ago

0.23.0

1 year ago

0.24.1

1 year ago

0.24.0

1 year ago

0.21.3

1 year ago

0.21.2

1 year ago

0.21.1

1 year ago

0.21.0

1 year ago

0.20.50

1 year ago

0.20.48

1 year ago

0.20.49

1 year ago

0.20.46

1 year ago

0.20.47

1 year ago

0.20.45

1 year ago

0.20.44

1 year ago

0.20.42

1 year ago

0.20.43

1 year ago

0.20.40

1 year ago

0.20.41

1 year ago

0.20.39

1 year ago

0.20.37

1 year ago

0.20.38

1 year ago

0.20.35

1 year ago

0.20.36

1 year ago

0.20.33

1 year ago

0.20.34

1 year ago

0.20.31

1 year ago

0.20.32

1 year ago

0.20.30

1 year ago

0.20.28

1 year ago

0.20.29

1 year ago

0.20.26

1 year ago

0.20.27

1 year ago

0.20.24

1 year ago

0.20.25

1 year ago

0.20.22

1 year ago

0.20.23

1 year ago

0.20.20

1 year ago

0.20.21

1 year ago

0.20.19

1 year ago

0.20.17

1 year ago

0.20.18

1 year ago

0.20.15

2 years ago

0.20.16

1 year ago

0.20.13

2 years ago

0.20.14

2 years ago

0.20.11

2 years ago

0.20.12

2 years ago

0.20.10

2 years ago

0.20.9

2 years ago

0.20.8

2 years ago

0.20.7

2 years ago

0.20.6

2 years ago

0.20.5

2 years ago

0.20.4

2 years ago

0.20.1

2 years ago

0.20.3

2 years ago

0.20.2

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.18.0

2 years ago

0.17.2

3 years ago

0.17.3

3 years ago

0.17.1

3 years ago

0.15.0

4 years ago

0.16.0

4 years ago

0.15.1

4 years ago

0.17.0

3 years ago

0.15.2

4 years ago

0.13.0

6 years ago

0.14.0

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.6

6 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

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.7

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago