1、全局安装 koa-generator
koa-generator 构建 koa 项目自动集成必备插件
2、构建初始化项目
生成的package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "name": "my-project", "version": "0.1.0", "private": true, "scripts": { "start": "node bin/www", "dev": "./node_modules/.bin/nodemon bin/www", "prd": "pm2 start bin/www", "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { "debug": "^4.1.1", "koa": "^2.7.0", "koa-bodyparser": "^4.2.1", "koa-convert": "^1.2.0", "koa-json": "^2.0.2", "koa-logger": "^3.2.0", "koa-onerror": "^4.1.0", "koa-router": "^7.4.0", "koa-static": "^5.0.0", "koa-views": "^6.2.0", "pug": "^2.0.3" }, "devDependencies": { "nodemon": "^1.19.1" } }
|
3、安装依赖并运行
1 2
| cd my-project npm install && npm run dev
|
4、安装koa2-cors
解决跨域
5、vercel 配置
入口文件
入口文件一定要是index.js,如果改写其他如 app.js,main.js 等等 vercel 均无法识别,这是一个大坑.
koa 监听端口不能是 3000 ,因为vercel 默认是 3000 端口