Express顶部的Mount Koajs应用程序 [英] Mount Koajs app on top of Express

查看:103
本文介绍了Express顶部的Mount Koajs应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自koajs.com:

From koajs.com:

app.callback()

返回适用于http.createServer()方法的回调函数来处理请求.您还可以使用此回调函数将koa应用程序安装在Connect/Express应用程序中.

Return a callback function suitable for the http.createServer() method to handle a request. You may also use this callback function to mount your koa app in a Connect/Express app.

现在我有一个Express应用程序,它已经启动了自己的http服务器.如何在现有服务器上安装Koa应用程序,使其共享相同的端口?

Now I have an Express app that already starts its own http server. How can I mount a koa app on top of this existing server, so that it shares the same port?

我会将koa应用程序包含为Express中间件吗?我仍然使用app.callback()吗?

Would I include the koa app as an Express middlware? Do I still use app.callback() for that?

推荐答案

expressapp.use(koaapp.callback())很好.但是请记住,koaapp.callback()没有next,因此在使用Express应用程序时不会传递任何错误,也不会跳过koaapp.

expressapp.use(koaapp.callback()) is fine. but remember, koaapp.callback() does not have a next, so there's no passing errors to the express app or skipping the koaapp once you use it.

最好将它们完全分开,因为它们的API不兼容

it's better to keep them completely separate since their APIs are incompatible

var koaapp = koa()
var expressapp = express()
http.createServer(req, res) {
  if (true) koaapp(req, res);
  else expressapp(req, res);
})

这篇关于Express顶部的Mount Koajs应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆