Async server
- Async server is a lightweight Express-like framework
- Build with native Node.js HTTP
Getting started
import { Server } from "async-server";
const app = new Server();
- Next, we will register a route called
/index
and write out "Hello World"
import { Server } from "async-server";
const app = new Server();
class IndexPage {
async GET(ctx) {
ctx.response = "Hello world";
}
}
app.route("/index", new IndexPage());
- Finally listen to localhost and port 80
import { NodeServer } from "async-server";
const app = new Server();
class IndexPage {
async GET(ctx) {
ctx.response = "Hello world";
}
}
app.route("/index", new IndexPage());
await app.listen(80);
- Go to http://localhost:8080/index and you should see the text "Hello World"
- Async server supports chaining so the code above can be shorten:
import { Server } from "async-server";
await new Server()
.route("/index", new class IndexPage {
async GET(ctx) {
ctx.response = "Hello world";
}
})
.listen(80);
JsonDB
- JsonDB is a type of local database which data is stored in a local
.json
file.
- JsonDB is fast and lightweight
Example
import { JsonDB } from "async-server";
const db = new JsonDB("Your json file path");
const User = db.schema("User", {
name: String,
id: Number
});
let user = new User({
name: "Reve",
id: 863068
});
await user.save();
let user1 = new User({
name: "Alex",
id: 509390
});
await user1.save();
await User.find({
name: "Alex"
}, 1).then(console.log);
await User.clear();
await db.clear();
- Important: Don't download Beta or Alpha versions