Koa -- 基于 Node.js 平台的下一代 web 开发框架
express 原班人马重构出来的
https://koa.bootcss.com/
快速开始
nvm install 7
cnpm install koa
# 编辑 my-koa-app.js 文件 , 写入代码
node my-koa-app.js
1
nvm install 7
2
cnpm install koa
3
# 编辑 my-koa-app.js 文件 , 写入代码
4
node my-koa-app.js
报错
cnpm install -g koa 全局安装模块 , 执行的时候找不到这个模块
$ node first.js
module.js:538
throw err;
^
Error: Cannot find module 'koa'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (E:Koafirst.js:1:75)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
x
1
$ node first.js
2
module.js:538
3
throw err;
4
^
5
6
Error: Cannot find module 'koa'
7
at Function.Module._resolveFilename (module.js:536:15)
8
at Function.Module._load (module.js:466:25)
9
at Module.require (module.js:579:17)
10
at require (internal/module.js:11:18)
11
at Object.<anonymous> (E:Koafirst.js:1:75)
12
at Module._compile (module.js:635:30)
13
at Object.Module._extensions..js (module.js:646:10)
14
at Module.load (module.js:554:32)
15
at tryModuleLoad (module.js:497:12)
16
at Function.Module._load (module.js:489:3)
安装结果的信息 :
C:UserspcAppDataRoaming
pm
ode_moduleskoa
cnpm i -g koa
Downloading koa to C:UserspcAppDataRoaming
pm
ode_moduleskoa_tmp
Copying C:UserspcAppDataRoaming
pm
ode_moduleskoa_tmp\_koa@2.4.1@koa to C:UserspcAppDataRoaming
pm
ode_moduleskoa
Installing koa's dependencies to C:UserspcAppDataRoaming
pm
ode_moduleskoa/node_modules
[1/24] content-disposition@~0.5.0 installed at node_modules\_content-disposition@0.5.2@content-disposition
[2/24] escape-html@~1.0.1 installed at node_modules\_escape-html@1.0.3@escape-html
[3/24] depd@^1.1.0 installed at node_modules\_depd@1.1.1@depd
[4/24] error-inject@~1.0.0 installed at node_modules\_error-inject@1.0.0@error-inject
[5/24] delegates@^1.0.0 installed at node_modules\_delegates@1.0.0@delegates
[6/24] content-type@^1.0.0 installed at node_modules\_content-type@1.0.4@content-type
[7/24] cookies@~0.7.0 installed at node_modules\_cookies@0.7.1@cookies
[8/24] fresh@^0.5.2 installed at node_modules\_fresh@0.5.2@fresh
[9/24] debug@* installed at node_modules\_debug@3.1.0@debug
[10/24] destroy@^1.0.3 installed at node_modules\_destroy@1.0.4@destroy
[11/24] koa-compose@^4.0.0 installed at node_modules\_koa-compose@4.0.0@koa-compose
[12/24] mime-types@^2.0.7 existed at node_modules\_mime-types@2.1.17@mime-types
[13/24] statuses@^1.2.0 existed at node_modules\_statuses@1.4.0@statuses
[14/24] koa-is-json@^1.0.0 installed at node_modules\_koa-is-json@1.0.0@koa-is-json
[15/24] http-errors@^1.2.8 installed at node_modules\_http-errors@1.6.2@http-errors
[16/24] http-assert@^1.1.0 installed at node_modules\_http-assert@1.3.0@http-assert
[17/24] parseurl@^1.3.0 installed at node_modules\_parseurl@1.3.2@parseurl
[18/24] only@0.0.2 installed at node_modules\_only@0.0.2@only
[19/24] vary@^1.0.0 installed at node_modules\_vary@1.1.2@vary
[20/24] on-finished@^2.1.0 installed at node_modules\_on-finished@2.3.0@on-finished
[21/24] accepts@^1.2.2 installed at node_modules\_accepts@1.3.4@accepts
[22/24] koa-convert@^1.2.0 installed at node_modules\_koa-convert@1.2.0@koa-convert
[23/24] type-is@^1.5.5 installed at node_modules\_type-is@1.6.15@type-is
[24/24] is-generator-function@^1.0.3 installed at node_modules\_is-generator-function@1.0.6@is-generator-function
All packages installed (36 packages installed from npm registry, used 2s, speed 206.78kB/s, json 35(43.75kB), tarball 347.06kB)
x
1
cnpm i -g koa
2
Downloading koa to C:UserspcAppDataRoaming
pm
ode_moduleskoa_tmp
3
Copying C:UserspcAppDataRoaming
pm
ode_moduleskoa_tmp\_koa .4.1 to C:UserspcAppDataRoaming
pm
ode_moduleskoa
4
Installing koa's dependencies to C:UserspcAppDataRoaming
pm
ode_moduleskoa/node_modules
5
[1/24] content-disposition~0.5.0 installed at node_modules\_content-disposition .5.2-disposition
6
[2/24] escape-html~1.0.1 installed at node_modules\_escape-html .0.3-html
7
[3/24] depd^1.1.0 installed at node_modules\_depd .1.1
8
[4/24] error-inject~1.0.0 installed at node_modules\_error-inject .0.0-inject
9
[5/24] delegates^1.0.0 installed at node_modules\_delegates .0.0
10
[6/24] content-type^1.0.0 installed at node_modules\_content-type .0.4-type
11
[7/24] cookies~0.7.0 installed at node_modules\_cookies .7.1
12
[8/24] fresh^0.5.2 installed at node_modules\_fresh .5.2
13
[9/24] debug* installed at node_modules\_debug .1.0
14
[10/24] destroy^1.0.3 installed at node_modules\_destroy .0.4
15
[11/24] koa-compose^4.0.0 installed at node_modules\_koa-compose .0.0-compose
16
[12/24] mime-types^2.0.7 existed at node_modules\_mime-types .1.17-types
17
[13/24] statuses^1.2.0 existed at node_modules\_statuses .4.0
18
[14/24] koa-is-json^1.0.0 installed at node_modules\_koa-is-json .0.0-is-json
19
[15/24] http-errors^1.2.8 installed at node_modules\_http-errors .6.2-errors
20
[16/24] http-assert^1.1.0 installed at node_modules\_http-assert .3.0-assert
21
[17/24] parseurl^1.3.0 installed at node_modules\_parseurl .3.2
22
[18/24] only .0.2 installed at node_modules\_only .0.2
23
[19/24] vary^1.0.0 installed at node_modules\_vary .1.2
24
[20/24] on-finished^2.1.0 installed at node_modules\_on-finished .3.0-finished
25
[21/24] accepts^1.2.2 installed at node_modules\_accepts .3.4
26
[22/24] koa-convert^1.2.0 installed at node_modules\_koa-convert .2.0-convert
27
[23/24] type-is^1.5.5 installed at node_modules\_type-is .6.15-is
28
[24/24] is-generator-function^1.0.3 installed at node_modules\_is-generator-function .0.6-generator-function
29
All packages installed (36 packages installed from npm registry, used 2s, speed 206.78kB/s, json 35(43.75kB), tarball 347.06kB)
根据信息找文件夹 , 能找到
执行程序 node E:Koafirst.js 第一行就报错
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
1
const Koa = require('koa');
2
const app = new Koa();
3
4
app.use(async ctx => {
5
ctx.body = 'Hello World';
6
});
7
8
app.listen(3000);
分析
全局安装的 cnpm i -g koa
我是先安装后执行的
已经安装了 , node 版本 8.9
解决方法
方法1 , 路径换成绝对路径
把
const Koa = require('koa');
换成
const Koa = require('C:/Users/pc/AppData/Roaming/npm/node_modules/koa');
或
const Koa = require('C:\Users\pc\AppData\Roaming\npm\node_modules\koa');
x
1
把
2
const Koa = require('koa');
3
换成
4
const Koa = require('C:/Users/pc/AppData/Roaming/npm/node_modules/koa');
5
或
6
const Koa = require('C:\Users\pc\AppData\Roaming\npm\node_modules\koa');
方法2 , 在和代码同级的目录下执行 cnpm i koa , 这样就不是全局安装了 , 然后不改代码的情况下运行代码
运行不报错
成功
logo
拓展资料 : 查 "require的入参怎么填"