koa2入门学习(1)-搭建一个简单的服务器

1. 新建目录koa-study,进入目录

2.下载koa2包 npm i koa

3.新建package.json:

 1 {
 2     "name": "hello-koa2",
 3     "version": "1.0.0",
 4     "description": "koa2 demo",
 5     "main": "app.js",
 6     "scripts": {
 7         "start": "node app.js"
 8     },
 9     "keywords": [
10         "koa",
11         "async"
12     ],
13     "author": "zhou peng",
14     "license": "Apache-2.0",
15     "repository": {
16         "type": "git",
17         "url": ""
18     },
19     "dependencies": {
20         "koa": "2.8.2"
21     }
22 }

4.新建app.js:

 1 const Koa = require('koa')
 2 const app = new Koa()
 3 
 4 app.use( async (ctx,next) => {
 5       await next()
 6       ctx.response.type = 'text/html'
 7       ctx.response.body = '<h2>koa2 study</h2>'      
 8 })
 9 
10 app.listen(3000)
   console.log('app started at port 3000...')

5.命令行npm start:

    启动成功

 

  

6.浏览器打开 localhost:3000

 

posted @ 2019-10-09 18:32  zoo-x  阅读(311)  评论(0编辑  收藏  举报