

Introduction to Koa.js
source link: https://www.tuicool.com/articles/hit/NVjMbun
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Koa.js is a minimal Node.js web framework developed by the team behind Express.js . Koa uses async functions, this gives you advantage over callback functions. By default Koa does not come with any middlewares. That makes Koa very minimal and elegant. In this post we’ll get started with building an API using Koa.js
Koa requires node v7.6.0 or higher for ES2015 and async function support.
Prerequisites
- Node.js Knowledge
- ES6 Syntax Familiarity
What are we building?
With the help of Koa, we’ll build a simplesentiment analysis API which takes a text as input and provides sentiment score as output. We’ll use following NPM packages to build this API.
- Koa - Web Framework
- Koa Router - For routing
- Koa Body Parser - To parse request body
- Sentiment - Analysing the text
Let’s get started building this API.
Hello World
We’ll start with a simplest example. First off, we need to install Koa. Create a new directory and we’ll run the following to install Koa.
yarn add koa
The hello world example is simple as it gets,
const Koa = require('koa'); const app = new Koa(); app.use(async ctx => { ctx.body = 'Hello World'; }); app.listen(3000, () => { console.log('Server started on localhost:3000'); });
First line is importing Koa. In the next line, we initialize the Koa application.
app.use(function)
is a middleware. This gets called for every request sent to the server. And we are setting the body as “Hello World”. Hence on every route, we’ll get the response “Hello World”. And finally we are listening on port number 3000.
Koa Middleware
It’s very easy to create a custom middleware in Koa. In the last section we used app.use(function)
, this function can be used to create a Koa middleware. Koa middleware flows in a stack like manner, allowing you to perform actions downstream then filter and manipulate the response upstream. Koa middleware are simple functions which return a MiddlewareFunction
with signature (ctx, next)
. When the middleware is run, it must manually invoke next()
to run the “downstream” middleware.
const Koa = require('koa'); const app = new Koa(); app.use(async (ctx, next) => { console.log('1'); await next(); console.log('2'); }); app.use(async (ctx, next) => { console.log('3'); await next(); console.log('4'); }); app.use(async (ctx, next) => { console.log('5'); ctx.body = 'Hello World'; console.log('6'); }); app.listen(3000, function(){ console.log('Server started on localhost:3000'); });
If you hit localhost:3000
on the browser, you’ll get following console output. The process goes like this,
next() next()
Server started on localhost:3000 1 3 5 6 4 2
Koa Middlewares can be used for Logging, Exception Handling, Authentication, and many more things. Here’s a list of middlewares from Koa Wiki .
Let’s move on to building sentiment analysis API.
Enter Sentiment
We’ll use a Node.js library called
sentiment
to calculate sentiment scores. This library performs AFINN-based sentiment analysis. It comes with a list of words with its predefined scores. For every sentence, it finds average sentiment scores of all words in the sentiment. It gives the score in the range of -5 to 5, here -5 being most negative and 5 being most positive. We’ll start with installing sentiment
.
yarn add sentiment
Let’s see an example of how it works
const Sentiment = require('sentiment'); const sentiment = new Sentiment(); let result = sentiment.analyze('Cats are amazing.'); console.log(result); /* { score: 4, comparative: 1.3333333333333333, tokens: [ 'cats', 'are', 'amazing' ], words: [ 'amazing' ], positive: [ 'amazing' ], negative: [] } */ result = sentiment.analyze('Cats are lazy'); console.log(result); /* { score: -1, comparative: -0.3333333333333333, tokens: [ 'cats', 'are', 'lazy' ], words: [ 'lazy' ], positive: [], negative: [ 'lazy' ] } */
Here’s score
is the sum of sentiment scores of all words, and comparative
is the average score. We’re interested in comparative
score.
Let’s integrate sentiment analysis with our Koa application.
Koa + Sentiment
We need to install koa-router
middleware for using routes in Koa and koa-bodyparser
for parsing request body. Let’s install these with,
yarn add koa-router koa-bodyparser
Now we are building the final API. We’ll use the following configuration for the API.
/analyze {"text": "The text to be analyzed"} {"text": "The text to be analyzed", "score": 0.3}
const Koa = require('koa'); const Router = require('koa-router'); const Sentiment = require('sentiment'); const bodyParser = require('koa-bodyparser'); const app = new Koa(); const router = new Router(); const sentiment = new Sentiment(); // Analyze a text and return sentiment score in the range of -1 to 1 function analyze(text) { const result = sentiment.analyze(text); const comp = result.comparative; const out = comp / 5; return out; } // Use bodyparser middleware to parse JSON request app.use(bodyParser()); // Define POST request route to analyze the text router.post('/analyze', async (ctx, next) => { // Look for text property on request body const text = ctx.request.body.text; if (text) { // Analyze the given text const score = analyze(text); // Send response ctx.body = { text, score }; } else { // Send error if there's not text property on the body ctx.status = 400; ctx.body = { "error": "Please provide a text to analyze" }; } }); // Use Koa Router middleware app .use(router.routes()) .use(router.allowedMethods()); // Finally, start the server app.listen(3000, function(){ console.log('Server started on localhost:3000'); });
That’s our Sentiment Analysis API. We’ll go through it line by line.
analyze() app.use(bodyParser()) /analyze /analyze listen
That wraps up our Sentiment Analysis API using Koa.js. Full code is available on this Github Repo . In Part 2 of Koa Tutorial we’ll cover Logging and adding Analytics to our Sentiment Analysis API. Subscribe to Newsletter to receive Part 2.
Recommend
-
65
-
46
原生HTTP服务器 学习过Nodejs的朋友肯定对下面这段代码非常熟悉: const http = require('http'); let server = http.createServer((req, res) => { // ....回调函数
-
44
花了两个多月时间,我与 lazzzis 完成了第二版本的Putong OJ,因为中间忙着春招以及毕业设计等,项目最近才正式上线。 项目线上地址:acm.cjlu.edu.cn/ 项目前端地址:github.com/acm309/Puto… 项目后端地址:git
-
68
-
45
Vue+Koa+Mongodb 小练习 作者: Pawn 本文首发: Pawn博客 功能: 基于vue koa mongodb进行登录,注册,留言的简单网站。 体验地址: demo.lcylove.cn github: github.com/LiChangy
-
37
目前使用的技术栈是:前端Vue、后端Koa、数据库Mongodb。 然而每当起服务的时候,都要 npm start 、 node ./server/app.js ,还要同时保持这两个窗口一直是开着的,很是麻烦。 而且因为我...
-
62
-
59
Koa 在众多NodeJs框架中,以短小精悍而著称,核心代码只有大约570行,非常适合源码阅读。 实际上核心来说,Koa主要是两块 ctx 本文就核心阅读中间件的源码。 Koa使用 中间件可以理...
-
43
从今天开始阅读学习一下 Koa 源码, Koa 对前端来说肯定不陌生,使用 node 做后台大部分会选择 Koa 来做, Koa 源码的代码量其实很少,接下来让我们一层层剥离,分析...
-
52
SQLite 是一种嵌入式数据库,它的数据库就是一个文件。 Sequelize 是一个基于 promise 的 Node.js ORM , 目前支持
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK