简介:

koa被称为基于Node.js平台的下一代web开发框架(上一代是Express),Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数,Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。

¶¶ 作者的电脑是Mac,全文的代码是Mac下的操作代码!

1. node环境

下面的是我电脑上已安装好的node环境,node安装相对简单,安装请自行百度!

~ node -v
v10.6.0

~ npm -v
6.1.0

2. 建立项目

新建并进入项目文件夹
mkdir koa2-demo && cd koa2-demo
创建一个node项目

tonglei@MacBook-Pro-2:/home/code/nodejs-demo/koa2-demo$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (koa2-demo)
version: (1.0.0)
description: koa2 demo
entry point: (index.js)
test command:
git repository:
keywords:
author: tonglei
license: (ISC)
About to write to /home/code/nodejs-demo/koa2-demo/package.json:

{
  "name": "koa2-demo",
  "version": "1.0.0",
  "description": "koa2 demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "tonglei",
  "license": "ISC"
}


Is this OK? (yes)

这样我们就完成了一个最简单的nodejs项目,在当前的目录下面生成了package.json的文件

tonglei@MacBook-Pro-2:/home/code/nodejs-demo/koa2-demo$ ll
total 8
drwxr-xr-x  3 tonglei  wheel  102  8 20 22:09 .
drwxr-xr-x  3 tonglei  wheel  102  8 20 22:04 ..
-rw-r--r--  1 tonglei  wheel  221  8 20 22:09 package.json

接下来,我们来安装koa2的库,到当前的项目中

tonglei@MacBook-Pro-2:/home/code/nodejs-demo/koa2-demo$ npm install koa -s
+ koa@2.5.2
added 41 packages from 21 contributors in 19.609s

查看当前目录,发现生成了一个node_modules的目录,这个目录存放了koa包所有的依赖的库。

tonglei@MacBook-Pro-2:/home/code/nodejs-demo/koa2-demo$ ll
total 32
drwxr-xr-x   5 tonglei  wheel    170  8 20 22:14 .
drwxr-xr-x   3 tonglei  wheel    102  8 20 22:04 ..
drwxr-xr-x  41 tonglei  wheel   1394  8 20 22:14 node_modules
-rw-r--r--   1 tonglei  wheel  10645  8 20 22:14 package-lock.json
-rw-r--r--   1 tonglei  wheel    266  8 20 22:14 package.json

然后,我们新建一个启动文件:index.js,用来启动Koa的项目

~$ cat index.js
var koa = require('./node_modules/koa');
var app = new koa();

app.use(function *(){
  this.body = 'Hello World';
});

app.listen(3000);

启动项目

~$ node index.js

打开浏览器 http://localhost:3000/

看到这界面,那么恭喜你,第一步成功了!

3,通过koa-generator来建立项目

安装koa-generator,这是一个命令工具,所以需要全局安装,第一次报无权限操作,第二次直接sudo

~$  npm install -g koa-generator
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/tonglei/.npm/_logs/2018-08-20T14_38_36_964Z-debug.log
~$  sudo npm install -g koa-generator
Password:
/usr/local/bin/koa -> /usr/local/lib/node_modules/koa-generator/bin/koa
/usr/local/bin/koa2 -> /usr/local/lib/node_modules/koa-generator/bin/koa2
+ koa-generator@1.1.16
added 6 packages from 5 contributors in 86.91s

进入开发目录,直接用koa2,创建项目

koa2 koa2-demo01

   create : koa2-demo01
   create : koa2-demo01/package.json
   create : koa2-demo01/app.js
   create : koa2-demo01/public
   create : koa2-demo01/public/stylesheets
   create : koa2-demo01/public/stylesheets/style.css
   create : koa2-demo01/routes
   create : koa2-demo01/routes/index.js
   create : koa2-demo01/routes/users.js
   create : koa2-demo01/views
   create : koa2-demo01/views/index.pug
   create : koa2-demo01/views/layout.pug
   create : koa2-demo01/views/error.pug
   create : koa2-demo01/public/images
   create : koa2-demo01/bin
   create : koa2-demo01/bin/www

   install dependencies:
     $ cd koa2-demo01 && npm install

   run the app:
     $ DEBUG=koa2-demo01:* npm start

   create : koa2-demo01/public/javascripts
 ~$ ll
total 16
drwxr-xr-x  8 tonglei  wheel  272  8 20 22:45 .
drwxr-xr-x  4 tonglei  wheel  136  8 20 22:45 ..
-rw-r--r--  1 tonglei  wheel  977  8 20 22:45 app.js
drwxr-xr-x  3 tonglei  wheel  102  8 20 22:45 bin
-rw-r--r--  1 tonglei  wheel  630  8 20 22:45 package.json
drwxr-xr-x  5 tonglei  wheel  170  8 20 22:45 public
drwxr-xr-x  4 tonglei  wheel  136  8 20 22:45 routes
drwxr-xr-x  5 tonglei  wheel  170  8 20 22:45 views

进入项目,并安装依赖库

cd koa2-web && npm install
npm WARN deprecated swig@1.4.2: This package is no longer maintained

> fsevents@1.2.4 install /home/code/nodejs-demo/koa2-web/node_modules/fsevents
> node install

[fsevents] Success: "/home/code/nodejs-demo/koa2-web/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile

> nodemon@1.18.3 postinstall /home/code/nodejs-demo/koa2-web/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

npm notice created a lockfile as package-lock.json. You should commit this file.
added 451 packages from 276 contributors and audited 1633 packages in 49.682s
found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details

启动项目

npm run start

> koa2-web@0.1.0 start /home/code/nodejs-demo/koa2-web
> node bin/www

打开浏览器 http://localhost:3000/ 见到下图内容,项目创建成功!

打赏

1 对 “node.js的web开发框架koa2初体验!”的想法;

发表评论

电子邮件地址不会被公开。