原创  koa入门之hello world

分类:前端 2021-02-23T17:00:55    163人阅读   

1、管理员身份打开cmder (或者cmd),跳转到项目目录,创建app 文件命令如下

F:\cmder_mini
λ E:
E:\
λ cd procedure\html\

E:\procedure\html                                                                                                                                                                                            
λ mkdir koa                                                                                                                                                                                                  
                                                                                                                                                                                                             
E:\procedure\html                                                                                                                                                                                            
λ cd koa                                                                                                                                                                                                     
                                                                                                                                                                                               
E:\procedure\html\koa                                                                                                                                                                                        
λ npm i koa                                                                                                                                                                                                  
npm WARN saveError ENOENT: no such file or directory, open 'E:\procedure\html\koa\package.json'                                                                                                              
npm notice created a lockfile as package-lock.json. You should commit this file.                                                                                                                             
npm WARN enoent ENOENT: no such file or directory, open 'E:\procedure\html\koa\package.json'                                                                                                                 
npm WARN koa No description                                                                                                                                                                                  
npm WARN koa No repository field.                                                                                                                                                                            
npm WARN koa No README data                                                                                                                                                                                  
npm WARN koa No license field.                                                                                                                                                                               
                                                                                                                                                                                                             
+ koa@2.13.1                                                                                                                                                                                                 
added 45 packages from 22 contributors in 8.419s                                                                                                                                                             
                                                                                                                                                                                                             
E:\procedure\html\koa                                                                                                                                                                                        
λ ls                                                                                                                                                                                                         
node_modules/  package-lock.json                                                                                                                                                                             
                                                                                                                                                                                                             
E:\procedure\html\koa                                                                                                                                                                                        
λ type nul>app.js

2、app.js 贴入如下代码

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

3、运行 

E:\procedure\html\koa
λ node app.js

4、浏览器打开http://127.0.0.1:3000



点击广告?(赏赐笔者)