推荐:https://juejin.cn/post/7032934725459279909
https://juejin.cn/post/6995063516470198279#heading-9
1.安装
初始化:npm install egg --type=simple
安装依赖: npm intall
启动: npm run dev
默认地址是 http://localhost:7001
修改启动地址、端口:config -> config.default.js
...module.exports = appInfo => {/*** built-in config* @type {Egg.EggAppConfig}**/const config = exports = {};// code startconfig.cluster = {listen: {path: '',port: 8001, // 端口hostname: '127.0.0.1', // 0.0.0.0}}// code endreturn {...config,};};...
2.跨域
安装跨域插件npm i egg-cors --save
配置config下的plugin.js
'use strict';/** @type Egg.EggPlugin */module.exports = {// had enabled by egg// static: {// enable: true,// }cors: {enable: true,package: 'egg-cors',},};
配置config下的config.default.js
// 关闭crsf,开启跨域config.security = {csrf: {enable: false,},domainWhiteList: [ ],};// 允许跨域方法config.cors = {origin: '*',allowMethods: 'GET, PUT, POST, DELETE, PATCH',};
