Skip to content
lixinyang123Public

About

Simple, Fast, Zero-dependency web framework for Node.js 🌐

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

67 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Plankton

NPM Version NPM Unpacked Size CI

Plankton is a simple, fast, zero-dependency web framework for Node.js

Features

  • Simple
  • Fast
  • Zero-dependency
  • Small size
  • Support mvc
  • Built-in template engine

Install

This is a Node.js module available through the npm.

npm install @lixinyang123/plankton
# or
yarn add @lixinyang123/plankton

Quick Start

You need create a nodejs project first.

yarn init
yarn add @lixinyang123/plankton
mkdir src
touch src/main.js

copy this code to src/main.js.

import plankton from "@lixinyang123/plankton";

plankton()
  .map("/", (req, res) => {
    res.end("hello world");
  })
  .start();

Finally, you can launch your first web app.

node src/main.js
curl localhost:8080

Usage

Here are some basic usages, You can find more in the example folder.

Basic

plankton()
  .map("/", (req, res) => {
    res.end("hello world");
  })
  .start();

Group

plankton()
  .mapGroup("api", (group) => {
    group.map("/", (req, res) => {
      res.end("hello world");
    });
  })
  .start();

Middleware

  • src/main.js
import plankton from "@lixinyang123/plankton";
import TestMiddleware from "./middlewares/test.js";

let app = plankton();

app.use(async (req, res, next) => {
  console.log("middleware 1 start");
  await next(req, res);
  console.log("middleware 1 end");
});

app.use(TestMiddleware);

app.map("/", (req, res) => {
  res.end("hello world");
});

app.start();
  • src/middlewares/test.js
export default async function (req, res, next) {
  res.write("middleware 2 start");
  await next(req, res);
  res.write("middleware 2 end");
}

curl localhost:8080, You can see the output

middleware 1 start
middleware 2 start
middleware 2 end
middleware 1 end

MVC

  • src/main.js
plankton().mapController().start();
  • src/controllers/home.js
export default {
  index: async (req, res) => {
    await res.render("index.ejs", { action: "index" });
  },

  about: async (req, res) => {
    await res.render("about.ejs", { action: "about" });
  },
};
  • src/views/index.ejs
<body>
  <h1>Hello world from action: <%= action %></h1>
</body>

Layout

  • src/views/_layout.ejs
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
  </head>
  <body>
    <header>Header</header>
    <%{{ content }}%>
    <footer>Footer</footer>
  </body>
</html>
  • src/views/home/index.ejs
<%{ '../_layout.ejs' }%> <%{ content %>
<div>
  <% for (let i = 0; i < 5; i++) { %>
  <h1>Hello <%= world %></h1>
  <% } %>
</div>
<%} %>
  • src/main.js
plankton()
  .map("/", async (req, res) => {
    await res.render("index.ejs", { world: "world" });
  })
  .start();

StaticFile

let app = plankton();

// map static file
app.useStaticFile();
app.useStaticFile("static", "test");

app.map("/", (req, res) => {
  res.redirect("/index.html");
});

app.start();

Cors

plankton()
  .useCors()
  .map("/", (req, res) => {
    res.end("hello world");
  })
  .start();

Logger

plankton()
  .useLogger((msg, level) => {
    // handler...
  })
  .map("/", (req, res) => {
    res.end("hello world");
  })
  .start();

Cookie

plankton()
  .map("/", (req, res) => {
    res.cookie("hello", "world");
    res.end("hello world");
  })
  .map("/test", (req, res) => {
    res.end("hello " + req.cookie["hello"]);
  })
  .start(8080);

res.cookie(key, value, expires, path)

Session

plankton()
  .useSession(/* 1000 * 5 */)
  .map("/", (req, res) => {
    res.session.set("hello", "world");
    res.end("hello world");
  })
  .map("/test", (req, res) => {
    res.end("hello " + req.session.get("hello"));
  })
  .start(8080);

default session expires is 30 mins

About

Simple, Fast, Zero-dependency web framework for Node.js 🌐

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages