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

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')
})

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理