芯が強い人になるESTJ-A

# Nodejs开发服务端api

IT開発 Tags: 无标签 阅读: 258

nodejs允许,JavaScript脱离浏览器,命令行运行。

一般小网站,类似微信后台,直接可以用,nodejs开发服务端提供数据,不需要java,python等语言。

通过nodejs+koa2+数据库框架=》操作数据库,提供数据源,展示信息。

koa2
ctx,next

中间件
new对象实例化。


const Koa = require('koa')

//new实例化,Koa,才能点出来
const app = new Koa()

app.use(async(ctx,next)=>{
    //async await,洋葱圈模型
    console.log(1)
    await next()
    console.log(2)
})

app.use(async(ctx,next)=>{
    console.log(3)
    await next()
    console.log(4)
})

app.listen(3000)

导入包,npm install,需要锁版本,直接导入package。json

"dependencies": {
    "axios": "^0.18.0",
    "basic-auth":"^2.0.1",
    "bcryptjs":"^2.4.3",
    "jsonwebtoken":"^8.4.0",
    "koa": "^2.7.0",
    "koa-bodyparser":"^4.2.1",
    "koa-router": "^7.4.0",
    "koa-static":"^5.0.0",
    "lodash": "^4.17.11",
    "mysql2": "^1.6.5",
    "require-directory":"^2.1.1",
    "sequelize": "^5.6.1",
    "validator":"^10.11.0"
  }
}