用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
```javasript const Koa = require('koa'); const logger = require('koa-logger'); const Router = require('koa-router'); const cors = require('@koa/cors'); const compose = require('koa-compose') const qs = require('qs'); //解析querystring const app = new Koa(); const Mock = require('mockjs'); //生成模拟数据 app.use(logger());//记录日志 app.use(cors());//支持跨域请求 // 主页(非RESTful格式的API) let routerHome = new Router(); routerHome.get('/', async (ctx, next) => { ctx.body = '欢迎欢迎!'; }) // RESTful API let routerRest = new Router(); //获取指定资源resource的列表 //例如:http://localhost:3000/resource routerRest.get('/resource', async (ctx, next) => { let map = qs.parse(ctx.querystring) || { pageSize: 3, page: 1 }; //生成模拟数据 let mockOption = { count: 28, totalPages: 3, pageSize: () => { return parseInt(map.pageSize || 10); }, currentPage: () => { return parseInt(map.page || 1); }, 'data|3': [ { 'id': '@increment()', 'company_id': '@increment()', //所在公司的ID,高校也是一个公司 "name": '@cname()', //联系人 "working_company|1": ['广州番禺职业技术学院', '中山大学', '华南师范大学'], //工作单位 "school_name|1": ['北京大学', '清华大学', '上海交通大学'], //毕业学校 "department": "信息工程学院", //院系专业 'grade_year': '@integer(2017,2019)', //毕业年份 "cover_image_id": 1, "cover_image": "http://via.placeholder.com/200x100", //照片 "abstract": "@cparagraph(5,10)", //简介 'major|1': ['计算机软件技术', '计算机应用', '电子商务'], 'tags': ['系统分析师', '教授', '生物技术', '模式识别', '天线技术', '生物制药', '化工原料'].join(','), 'contact': /^1[38][1-9]\d{8}/, //联系电话 'email': '@email()', "gender|1": ['男', '女'], "city": "@city(true)", //居住城市 "diplomas|1": ["博士", "硕士", "专科", "本科", "中职"], //学历 "date": "@date()", "description": "@cparagraph(5,10)", //详细描述 "job_requirements": "@cparagraph(3, 5)", //任职要求 "address": "@county(true)", //工作地点 "type": () => { if (map.type == "" || map.type == '全部') { let list = ["兼职", "实习", "全职"]; return list[Mock.Random.integer(0, list.length - 1)]; } else { return map.type } }, //知识画像,学生所学的课程 "courses": () => { let array = ['计算机文化基础', 'C语言程序设计基础', '离散数学', '高等数学', 'Vuejs前端开发技术基础', '大学英语', '微机原理', '电子电路基础', '人工智能' ]; //随机选择3个标签 array.sort(function (a, b) { return Math.random() > 0.5 ? 1 : 0 }); //简单打乱方法 let [...rest] = array; return rest.join(','); }, //能力画像,专家的资格证书 "certification": () => { let array = ['高级程序员', '系统分析师', '国家计算机等级考试四级', '大学英语CET-4']; //随机选择3个标签 array.sort(function (a, b) { return Math.random() > 0.5 ? 1 : 0 }); //简单打乱方法 let [...rest] = array; return rest.join(','); }, }], }; let data = await Mock.mock(mockOption); ctx.body = { errno: 0, errmsg: 'POST API执行成功', data: data }; }) //获取指定ID的资源 //例如:http://localhost:3000/resource/1?name=zengqs&age=18 routerRest.get('/resource/:id', async (ctx, next) => { let id = ctx.params.id; //生成模拟数据 let mockOption = { //'id': '@increment()', 'id': id, 'company_id': '@increment()', //所在公司的ID,高校也是一个公司 "name": '@cname()', //联系人 "working_company|1": ['广州番禺职业技术学院', '中山大学', '华南师范大学'], //工作单位 "school_name|1": ['北京大学', '清华大学', '上海交通大学'], //毕业学校 "department": "信息工程学院", //院系专业 'grade_year': '@integer(2017,2019)', //毕业年份 "cover_image_id": 1, "cover_image": "http://via.placeholder.com/200x100", //照片 "abstract": "@cparagraph(5,10)", //简介 'major|1': ['计算机软件技术', '计算机应用', '电子商务'], 'tags': ['系统分析师', '教授', '生物技术', '模式识别', '天线技术', '生物制药', '化工原料'].join(','), 'contact': /^1[38][1-9]\d{8}/, //联系电话 'email': '@email()', "gender|1": ['男', '女'], "city": "@city(true)", //居住城市 "diplomas|1": ["博士", "硕士", "专科", "本科", "中职"], //学历 "date": "@date()", "description": "@cparagraph(5,10)", //详细描述 "job_requirements": "@cparagraph(3, 5)", //任职要求 "address": "@county(true)", //工作地点 "type": () => { let list = ["兼职", "实习", "全职"]; return list[Mock.Random.integer(0, list.length - 1)]; }, //知识画像,学生所学的课程 "courses": () => { let array = ['计算机文化基础', 'C语言程序设计基础', '离散数学', '高等数学', 'Vuejs前端开发技术基础', '大学英语', '微机原理', '电子电路基础', '人工智能' ]; //随机选择3个标签 array.sort(function (a, b) { return Math.random() > 0.5 ? 1 : 0 }); //简单打乱方法 let [...rest] = array; return rest.join(','); }, //能力画像,专家的资格证书 "certification": () => { let array = ['高级程序员', '系统分析师', '国家计算机等级考试四级', '大学英语CET-4']; //随机选择3个标签 array.sort(function (a, b) { return Math.random() > 0.5 ? 1 : 0 }); //简单打乱方法 let [...rest] = array; return rest.join(','); }, } let data = await Mock.mock(mockOption); ctx.body = { errno: 0, errmsg: 'POST API执行成功', data: data }; }) //新增记录,数据通过BODY传递 routerRest.post('/resource', async (ctx, next) => { ctx.body = { errno: 0, errmsg: 'POST API执行成功', data: '返回的数据' }; }) //删除指定ID的数据 routerRest.delete('/resource/:id', async (ctx, next) => { let id = ctx.params.id; ctx.body = { errno: 0, errmsg: 'POST API执行成功', data: '返回的数据' }; }) //更新指定ID的数据,数据通过BODY传递 routerRest.put('/resource/:id', async (ctx, next) => { let id = ctx.params.id; ctx.body = { errno: 0, errmsg: 'POST API执行成功', data: '返回的数据' }; }) app.use(compose([routerHome.routes(), routerHome.allowedMethods()])) app.use(compose([routerRest.routes(), routerRest.allowedMethods()])) //监听3000端口 app.listen(3000, () => { console.log('server is running at http://localhost:3000') }); ```