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')
})
发表回复