By default Koa does not come with any middlewares. maxage Browser cache max-age in milliseconds. (defaults to 0). Note that we’re calling an external URL within its callback function to get the random users array and send it directly to our koa-ejs template (we’ll build it in a few minutes). The following example creates a router as a module, loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app. ; immutable Tell the browser the resource is immutable and can be cached indefinitely. That makes Koa very minimal and elegant. The koa-router is the Express routing system equivalent. (defaults to false). Two common routing options are koa-route and koa-router . Koa requires node v7.6.0 or higher for ES2015 and async function support. Inclusion of features such as templating, sending files and JSONP support in the library’s core; Even though Express and Koa do almost the same thing and share the same author, they have different approaches to how middleware and routing are handled. 现在我们遇到第一个问题:koa这个包怎么装,app.js才能正常导入它? 方法一:可以用npm命令直接安装koa。先打开命令提示符,务必把当前目录切换到hello-koa这个目录,然后执行命令: C:\...\hello-koa> npm install koa@2.0.0 Static file serving middleware. We need a middleware to serve this purpose. Yes, it comes within a second package that needs to be added to your package.json separately. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”. Installation $ npm install koa-send Options. Koa requires node v7.6.0 or higher for ES2015 and async function support. You can think of Koa.js as a new and improved Express. In this tutorial, you’ll learn how to develop a RESTful API with Koa 2 and Postgres. Build a Sample App with Koa.js Koa uses async functions, this gives you advantage over callback functions. You’ll also be taking advantage of async/await functions, from ES2017, and test driven development (TDD).. koa-send. Thus, the order of types you supply is important. Express, by default doesn't allow you to serve static files. ; hidden Allow transfer of hidden files. Koa doesn’t have its own router bundled with it, so we’ll have to use a router middleware to handle routing on Koa apps. No bodyparser, and no router either. Unlike Express, Koa comes with almost nothing out of the box. Prerequisites Node.js Knowledge Create a new directory, public. For example, if I want to send an email to the merchant about an update on their app usage, can I access their details (with their permission, of course) via the same Koa session / verifyRequest calls — or do I need to 'manually' provide a way for them to link an account on … This tutorial requires Node v7.6.0 or greater. ; root Root directory to restrict file access. (defaults to false). In this post we'll get started with building an API using Koa.js. If no matches are found, a false is returned, and you should send a 406 "Not Acceptable" response to the client. ±å…¥å­¦ä¹ express,然后顺便再和koa2的实现方式对比一下。 老实说,还没看express.js源码之前,一直觉得express.js还是很不错的,无论从api设计,还是使用上都 … Static files are files that clients download as they are from the server. Routing. In the case of missing accept headers where any type is acceptable, the first type will be returned.