带路由器和正文解析器的最小 Koa.JS 示例
这是我用作新 NodeJS Web 服务器应用程序模板的最小 Koa.JS 应用程序。
minimal_koa_server.js
#!/usr/bin/env node
const router = require('koa-router')();
const koaBody = require('koa-body');
const Koa = require('koa');
const app = new Koa();
app.use(koaBody());
router.get('/', async ctx => {
ctx.body = "Hello world";
});
app.use(router.routes());
if (!module.parent) app.listen(3000);使用以下命令安装依赖
install_koa_deps.sh
npm i --save koa koa-router koa-body并使用以下命令运行
run_index.js
node index.js假设你已将上面的代码保存在 index.js 中。
现在(node index.js 命令仍在运行)转到 http://localhost:3000。你应该在那里看到 Hello world。现在轮到你继续努力开发世界上最伟大的 Web 服务器了 :-)
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow