简体   繁体   English

Expressjs到Koajs-路由和模板?

[英]Expressjs to Koajs - routing and templates?

I am looking to migrate my Express app to Koa but I have no idea how to get the route and template working in Koa. 我希望将Express应用程序迁移到Koa,但是我不知道如何在Koa中使用路由和模板。 It seems that there is documentation about this on their official site. 似乎在他们的官方网站上有关于此的文档。

For instance this is how I would do it in Express: 例如,这就是我在Express中的处理方式:

var express = require('express');
var app = express();

// respond with "Hello World!" on the homepage
app.get('/', function (req, res) {
    res.send('Hello World!');
});

// accept POST request on the homepage
app.post('/', function (req, res) {
    res.send('Got a POST request');
});

// accept PUT request at /user
app.put('/user', function (req, res) {
    res.send('Got a PUT request at /user');
});

// accept DELETE request at /user
app.delete('/user', function (req, res) {
    res.send('Got a DELETE request at /user');
})

How is this done in Koa then? 那么在Koa中如何做到这一点? What I get from their doc: 我从他们的文档中得到了什么:

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

Also, when comes to the template rendering, in Express: 另外,当涉及到模板渲染时,在Express中:

var express = require('express');
var router = express.Router();

router.get("/make", function(req, res) {

    return res.render("streams/make", {...});
});

So, how is this done in Koa? 那么,这在Koa中如何完成?

Any ideas? 有任何想法吗?

Koa doesn't provide this kind of thing out of the box - it's designed to be a little more low level than Express. Koa并没有提供这种现成的东西-它的设计水平比Express低。

You'll need to either manually write your own middleware handling routing (via ctx.path and ctx.method ) and templating (grab the template engine from NPM and render to ctx.body ), or use one of the many pre-built solutions . 您将需要手动编写自己的中间件处理路由(通过ctx.pathctx.method )和模板化(从NPM抓取模板引擎并渲染到ctx.body ),或者使用许多预先构建的解决方案之一

As mentioned already by Joe, Koa doesn't come with any middleware preinstalled. 正如乔已经提到的那样,Koa并未预装任何中间件。

For your needs, it seems like you would want to look into using koa-router and koa-views . 对于您的需求,您似乎想研究使用koa-routerkoa-views

Usage: 用法:

app.js app.js

const Koa = require('koa')
const views = require('koa-views')
const router = require('./routes')

const app = new Koa()

app.use(views(`${__dirname}/views`, { extension: 'pug' })) // You can replace 'pug' with whatever templating engine you're using.
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000)

module.exports = app

routes.js routes.js

const Router = require('koa-router')

const router = new Router()

router.get('/', async ctx => {
  ctx.body = 'Hello World!'
})

// accept POST request on the homepage
router.post('/', async ctx => {
  ctx.body = 'Got a POST request'
})

// accept PUT request at /user
router.put('/user', async ctx => {
  ctx.body = 'Got a PUT request at /user'
})

// accept DELETE request at /user
router.delete('/user', async ctx => {
  ctx.body = 'Got a DELETE request at /user'
})

// Render a template
router.get("/make", async ctx => {
  await ctx.render("streams/make", { ... });
})

module.exports = router

With Koa, instead of getting the req and res as parameters, you just get a ctx . 使用Koa,无需获取reqres作为参数,而只需获取ctx With this ctx object, you can access the request via ctx.request and the response with ctx.response . 有了这个ctx对象,你可以通过访问请求ctx.request并与响应ctx.response ctx.body is just an alias for ctx.response.body . ctx.body只是一个别名ctx.response.body Koa defines a lot of aliases for you, which you can read about here . Koa为您定义了很多别名,您可以在此处阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM