赞
踩
git 忽略文件配置
const Koa = require('koa')
const app = new Koa
app.use((ctx,next)=>{
ctx.body = 'hello api'
})
app.listen(3000,()=>{
console.log('server is running on http://localhost:3000');
})
启动
node main.js
访问
http://localhost:3000
nodemon工具
npm install nodemon -D
编写启动脚本 “dev”:“nodemon ./src/main.js”,
输入命令启动 npm run dev
文件改动后会自动重启服务
{ "name": "node-builds-api-service-n", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "dev":"nodemon ./src/main.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://gitee.com/Ashes_of_time_y/node-builds-api-service-n.git" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "koa": "^2.13.4", "nodemon": "^2.0.20" } }
// config.default.js
const dotenv = require('dotenv')
dotenv.config()
console.log(process.env.APP_PORT);
module.exports =process.env
const {APP_PORT} = require('./config/config.default.js')
// main.js
const Koa = require('koa')
const {APP_PORT} = require('./config/config.default.js')
const app = new Koa
app.use((ctx,next)=>{
ctx.body = 'hello api1111111'
})
app.listen(APP_PORT,()=>{
console.log(`server is running on http://localhost:${APP_PORT}`);
})
npm run dev
根据不同url调用不同处理函数,返回不同数据
npm install
// 简单路由 // main.js const Koa = require('koa') const {APP_PORT} = require('./config/config.default.js') const app = new Koa // 路由 const Router = require('koa-router') const indexRouter = new Router() indexRouter.get('/',(ctx,next)=>{ ctx.body = 'hello index' }) const userRouter= new Router() userRouter.get('/users',(ctx,next)=>{ ctx.body = 'hello users' }) // 注册中间件 // app.use((ctx,next)=>{ // ctx.body = 'hello api11111' // }) app.use(indexRouter.routes()) app.use(userRouter.routes()) app.listen(APP_PORT,()=>{ console.log(`server is running on http://localhost:${APP_PORT}`); })
src目录下面创建user.route.js
//user.route.js
const Router = require('koa-router')
const router = new Router({prefix:'/users'})
router.get('/',(ctx,next)=>{
ctx.body = 'hello users'
})
module.exports = router
// main.js
const Koa = require('koa')
const {APP_PORT} = require('./config/config.default.js')
const userRouter =require('./router/user.route.js')
const app = new Koa
app.use(userRouter.routes())
app.listen(APP_PORT,()=>{
console.log(`server is running on http://localhost:${APP_PORT}`);
})
src目录下面新建APP目录,APP目录下面新建index.js
// app/index.js
const Koa = require('koa')
const userRouter =require('../router/user.route.js')
const app = new Koa()
app.use(userRouter.routes())
module.exports = app
// 改写main.js
const {APP_PORT} = require('./config/config.default.js')
const app = require('./app/index.js')
app.listen(APP_PORT,()=>{
console.log(`server is running on http://localhost:${APP_PORT}`);
})
路由:解析url,不同的路由分发给对应的控制器方法
控制器:处理不同的业务
user.controller.js
// user.controller.js
class UserController{
async register(ctx,next){
ctx.body = "用户注册成功!"
}
async login(ctx,next){
ctx.body = "登录成功!"
}
}
module.exports = new UserController()
//user.route.js const Router = require('koa-router') const {register,login} =require('../controller/user.controller') const router = new Router({prefix:'/users'}) // router.get('/',(ctx,next)=>{ // ctx.body = 'hello users' // }) // 注册接口 router.get('/register',register) // 登录接口 router.get('/login',login) // 测试 // http://localhost:8000/users/register // http://localhost:8000/users/login module.exports = router
// app/index.js
const Koa = require('koa')
// 1.引入koabody
const KoaBody = require('koa-body')
const userRouter =require('../router/user.route.js')
const app = new Koa()
// use koabody
app.use(KoaBody())
app.use(userRouter.routes())
module.exports = app
// user.controller.js class UserController{ async register(ctx,next){ // 1.获取数据 console.log(ctx.request.body); //使用koabody // 2.操作数据库 // 3.返回数据 ctx.body = ctx.request.body+'我草!' } async login(ctx,next){ ctx.body = "登录成功!" } async baidu_map(ctx,next){ ctx.body = "bauidu地图" } } module.exports = new UserController()
创建service文件夹
创建user.service.js文件
class UserSevice {
async createUesr(username,password){
//todo...
return '创建用户成功!'
}
}
module.exports = new UserSevice()
// user.controller.js const {createUesr} = require('../service/user.service') class UserController{ async register(ctx,next){ // 1.获取数据 // console.log(ctx.request.body); const {username,userpassword} = ctx.request.body console.log(username,userpassword); // 2.操作数据库 const res = await createUesr(username,userpassword) console.log(res,"rse"); // 3.返回数据 ctx.body = {username,userpassword} } async login(ctx,next){ ctx.body = "登录成功!" } async baidu_map(ctx,next){ ctx.body = "bauidu地图" } } module.exports = new UserController()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。