HapticX / happyx

Macro-oriented asynchronous web-framework written in Nim with ♥
https://hapticx.github.io/happyx/
MIT License
545 stars 19 forks source link

NodeJS Bindings 🔥 #157

Open Ethosa opened 1 year ago

Ethosa commented 1 year ago

Goals 📃

Examples 👨‍🎓

ECHO Server 👋

import {Server} from "happyx";

// by default host and port is 127.0.0.1:5000 so you may to call it without args
let server = new Server("127.0.0.1", 5000);

server.get("/", (req) => {
  console.log(req);
  return "Hello, world!";
});

server.start()

WebSockets 🔌

server.ws("/sockets", (ws) => {
  console.log(ws.state);
  if (ws.state == WebSocketState.Open && ws.data == "ping") {
    ws.send("pong");
  }
});

Static files 📁

server.static("/route", "./directory")

Mount Other Apps 🛠

const main = new Server();
const users = new Server();

main.mount("/users", users);

// at http://127.0.0.1:5000/users/
users.get("/", () => {return 1});

Request Models 👨‍🔬

const UserModel = new RequestModel('UserModel', {
  username: ''
});

app.post('/[u:UserModel]', (req: Request) => {
  console.log(req.params.u.username)
});

Custom Path Param Types

newPathParamType("myType", /\d\d/, (data: string) => {
    return Number(data[0]) + Number(data[1]);
});

app.get("/test/custom/types{a:myType}", (req: Request) => {
    return req.params.a;
})
Ethosa commented 1 year ago

Now with hpx CLI you can create JavaScript/TypeScript projects

georgelemon commented 1 year ago

This is great! I'm curious about numbers, any benchmarks?

Ethosa commented 1 year ago

@georgelemon I'm try to open https://github.com/the-benchmarker/web-frameworks/pull/6798 but server doesn't want to start 🤔

waghanza commented 1 year ago

Results are out https://web-frameworks-benchmark.netlify.app/result?f=happyx but not very satisfying (internal issues on this benchmarking project)