语法
2024年10月14日
一、Methods
1.1 koa
const Koa = require("koa");
const app = new Koa();
app.use((ctx) => {
ctx.type = "application/json";
ctx.body = JSON.stringify({
data: "Hello World!",
});
});
app.listen(8000, () => {
console.log("Koa HTTP Server is running on port 8000");
});
1.2 koa http.createServer
Koa
使用 http.createServer()
方法来创建 HTTP
服务器。通常会看到 Koa
通过 app.callback()
方法将 Koa
应用程序转换为一个可以处理 HTTP
请求的回调函数,http.createServer()
使用这个回调来处理请求。
const Koa = require("koa");
const http = require("http");
const app = new Koa();
const server = http.createServer(app.callback());
app.use((ctx) => {
ctx.type = "application/json";
ctx.body = JSON.stringify({
data: "Hello World!",
});
});
server.listen(8000, () => {
console.log("Koa HTTP Server is running on port 8000");
});
二、Get
2.1 koa
const Koa = require("koa");
const KoaBody = require("koa-body");
const KoaRouter = require("koa-router");
const PORT = 3000;
const app = new Koa();
const { koaBody } = KoaBody;
const router = new KoaRouter();
router.get("/", (ctx) => {
ctx.body = "Hello, Koa!";
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});
2.2 koa http.createServer
const Koa = require("koa");
const HTTP = require("http");
const KoaBody = require("koa-body");
const KoaRouter = require("koa-router");
const PORT = 3000;
const app = new Koa();
const { koaBody } = KoaBody;
const router = new KoaRouter();
const server = HTTP.createServer(app.callback());
router.get("/", (ctx) => {
ctx.body = "Hello, Koa!";
});
app.use(router.routes()).use(router.allowedMethods());
server.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});
三、Post
3.1 koa
const Koa = require("koa");
const KoaBody = require("koa-body");
const KoaRouter = require("koa-router");
const PORT = 3000;
const app = new Koa();
const { koaBody } = KoaBody;
const router = new KoaRouter();
router.get("/", (ctx) => {
ctx.body = "Hello, Koa!";
});
router.post("/upload", async (ctx) => {
ctx.body = {
code: 200,
data: fileUrl,
message: "File uploaded",
};
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});
3.2 koa http.createServer
const Koa = require("koa");
const HTTP = require("http");
const KoaBody = require("koa-body");
const KoaRouter = require("koa-router");
const PORT = 3000;
const app = new Koa();
const { koaBody } = KoaBody;
const router = new KoaRouter();
const server = HTTP.createServer(app.callback());
router.get("/", (ctx) => {
ctx.body = "Hello, Koa!";
});
router.post("/upload", async (ctx) => {
ctx.body = {
code: 200,
data: fileUrl,
message: "File uploaded",
};
});
app.use(router.routes()).use(router.allowedMethods());
server.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});
四、WebSocket
const WS = require("ws");
const Koa = require("koa");
const HTTP = require("http");
const PORT = 3000;
const app = new Koa();
const clients = new Set();
const server = HTTP.createServer(app.callback());
const wss = new WS.Server({ server });
wss.on("connection", (ws) => {
clients.add(ws);
ws.on("message", (message) => {
console.log("Websocket message: ", message)
});
ws.on("close", () => {
clients.delete(ws);
});
});
server.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});