关于koa.js:koa的基本使用

6次阅读

共计 611 个字符,预计需要花费 2 分钟才能阅读完成。

1、初始化 package.json
npm init
2、装置 koa2
npm install koa
3、hello 代码

ctx.body="hello"必须写,否则页面呈现Not Found

const koa =require('koa')
const app = new koa()
app.use(async (ctx)=>{ctx.body="hello"})
app.listen(3000)
4、代码运行

批改代码时,必须从新运行
index.js 为须要运行的文件

node index.js
5、koa-static 动态资源加载

首先装置 koa-static 插件npm install koa-static

const Koa = require('koa')
const path = require('path')
const static = require('koa-static')

const app = new Koa()

// 动态资源目录对于绝对入口文件 index.js 的门路
const staticPath = './static'

app.use(static(path.join( __dirname,  staticPath)
))

app.use(async ( ctx) => {ctx.body = 'hello world'})

app.listen(3000, () => {console.log('[demo] static-use-middleware is starting at port 3000')
})
正文完
 0