egg.js
egg Egg.js 为企业级框架和应用而生的渐进式node.js框架
具体查看官方文档
https://eggjs.org/zh-cn/intro/quickstart.html
安装egg
npm init egg --type=simple
导入相关依赖
npm install
安装插件
安装模板引擎
npm i egg-view-ejs --save
安装mysql
npm i egg-mysql --save
安装跨域
npm i egg-cors --save
相关配置
config/config.default.js
/* eslint valid-jsdoc: "off" */
'use strict';
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1605233750598_6225';
// add your middleware config here
config.middleware = [];
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
//配置模板引擎
config.view = {
defaultViewEngine: 'ejs', // 默认使用的模板引擎
mapping: {
//'.ejs': 'ejs', // 找出ejs后缀的文件使用ejs语法
'.html': 'ejs' // 找出html后缀的文件使用ejs语法
}
}
//配置mysql
config.mysql = {
client: {
// host
host: '127.0.0.1',
// 端口号
port: '3306',
// 用户名
user: 'root',
// 密码
password: '123456',
// 数据库名
database: 'wangyi',
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
}
//配置跨域
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['*'], // 白名单
};
config.cors = {
origin: '*', // 如果不写origin则会按照白名单中的域名允许跨域,
// * 代表允许所有的域名进行跨域请求
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
}
return {
...config,
...userConfig,
};
};
config/plugin.js
'use strict';
/** @type Egg.EggPlugin */
module.exports = {
// had enabled by egg
// static: {
// enable: true,
// }
//配置egg-view-ejs模板引擎
ejs: {
enable: true,
package: 'egg-view-ejs'
},
//配置mysql的扩展
mysql: {
enable: true,
package: 'egg-mysql',
},
//配置跨域
cors: {
enable: true,
package: 'egg-cors'
}
};
egg操作数据库
https://blog.csdn.net/yzwdzkn/article/details/99622238
js模板引擎
art-template
https://aui.github.io/art-template/zh-cn/index.html
nest
Nest.js是一个渐进的Node.js框架,用于搭建后端服务器。