TheVegeta / yume

Yume is a lightweight and exceptionally fast web framework specifically designed for building efficient API servers.

Home Page:https://thevegeta.github.io/yume/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Yume (夢)

Yume is a lightweight web framework designed to build efficient API servers. It achieves this by leveraging the power of uWebSockets.js, a high-performance WebSocket library for Node.js.

Quick start

Install with NPM

npm i yume-server

Documentation

Hello Yume

const yume = new Yume();
const PORT = process.env.PORT || 8080;

yume.get("/", (req, res) => {
  res.json({ hello: "Yume 夢" });
});

yume.listen(8080, () => {
  console.log(`>started @${PORT}`);
});

Applying middleware

yume.use((req, res, next) => {
  console.log("Hello from middleware");
  next();
});

Dynamic Routes

yume.get("/post/:id", async (req, res) => {
  const { id } = req.getParams();
  return res.json({ id });
});

Query Parameter

// ?page=2&limit=3
yume.get("/post", async (req, res) => {
  const { page, limit } = req.query();
  return res.json({ page, limit });
});

Multipart upload

yume.post("/upload", async (req, res) => {
  const filesArray = await req.file();

  filesArray?.map((file: MultipartField) => {
    // do something magical
  });

  res.json({ status: true, msg: "file uploaded successfully", data: {} });
});

cors

yume.use((req, res, next) => {
  const origin = "https://example.com";
  res.writeHeader("Access-Control-Allow-Origin", origin);
  res.writeHeader("Access-Control-Allow-Methods", "GET, POST");
  next();
});

Handling 404 route

yume.notFound((req, res) => {
  res.status(404).end("Not found");
});

Handling globle error

yume.error((err, req, res) => {
  console.error(err);
  res.status(500).end("Internal server error");
});

License

Licensed under MIT License.

About

Yume is a lightweight and exceptionally fast web framework specifically designed for building efficient API servers.

https://thevegeta.github.io/yume/

License:MIT License


Languages

Language:TypeScript 100.0%