班级网站建设的内容,房产网站建设产品,广告联盟推广,上海高端网站设计node.js通过Sequelize 连接MySQL一.通过koa2脚手架构建项目1.1 安装koa-generator在终端输入#xff1a;$ npm install -g koa-generator11.2 使用koa-generator生成koa2项目$ koa2 HelloKoa21成功创建项目后#xff0c;进入项目目录#xff0c;并执行npm install命令$ cd H…node.js通过Sequelize 连接MySQL一.通过koa2脚手架构建项目1.1 安装koa-generator在终端输入$ npm install -g koa-generator11.2 使用koa-generator生成koa2项目$ koa2 HelloKoa21成功创建项目后进入项目目录并执行npm install命令$ cd HelloKoa2$ npm install121.3 启动项目在终端输入$ npm start1项目启动后默认端口号是3000在浏览器中运行可以得到下图的效果说明运行成功。二.创建连接2.1刚刚创建的文件使用webstorm打开新建一个db目录2.2查看Sequelize文档使用npm安装Sequelizenpm install --save sequelize1你还必须手动为所选数据库安装驱动程序选择一个方法之一# 选择以下之一:$ npm install --save pg pg-hstore # Postgres$ npm install --save mysql2$ npm install --save mariadb$ npm install --save sqlite3$ npm install --save tedious # Microsoft SQL Server123456我这里下载得是MySQL22.3连接数据库再刚刚创建得db文件加里面添加**config.js**添加连接代码module.exports { dbsMysql: mysql://root:123456localhost:3306/new //root是数据库管理员账号‘123546’是密码 3306是端口号(MySQL默认是3306) school_admin是数据库名称}123456继续在db文件夹里面添加mysql.js添加连接以及添加日记const Sequelize require(sequelize);const mysqlurl require(./config).dbsMysqlconst sequelize new Sequelize(mysqlurl, { // 选择一种日志记录参数 logging: console.log // 默认值,显示日志函数调用的第一个参数});// //每次启动server刷新数据库// (async (){// await sequelize.sync({ force: true });// })() module.exports sequelize1234567891011121314三.创建模型3.1模型定义在db目录下添加models文件夹再添加一个new2.js定义模型const { Sequelize, DataTypes, Model } require(sequelize);const sequelize require(../mysql);const new2 sequelize.define(t_new2, { name: { type: DataTypes.STRING, allowNull: false }, }, { // 这是其他模型参数 freezeTableName: true });// 定义的模型是类本身module.exports new2123456789101112131415四.添加路由4.1创建new2路由在routes文件夹中添加new2.js//引入kob得routes模块const router require(koa-router)()//定义模型为刚刚创建得new2.jslet Model require(../db/models/new2);//正常来说启动端口为http://localhost:3000 添加/new2就可以进入new2路由router.prefix(/new1)// 进入new2路由以后可以打印this is a users response!router.get(/, function (ctx, next) { ctx.body this is a users response!})//设置增加add接口router.post(/add, async function (ctx, next) { console.log(ctx.request.body) const new2 await Model.create(ctx.request.body); ctx.body { code:200, data:new2 }})//设置查询find接口router.post(/find, async function (ctx, next) { const new2 await Model.findAll({include: []}) console.log(1111) ctx.body { code: 200, data: new2 }})//设置通过id得到所需信息的get接口router.post(/get, async function (ctx, next) { // let users await User. // find({}) console.log(ctx.request.body) let new2 await Model.findOne({ // attributes: [name, where] where: { id: ctx.request.body.id } }); ctx.body { code:200, data:new2 }})//设置修改update接口router.post(/update, async function (ctx, next) { console.log(ctx.request.body) // let pbj await Model.update({ _id: ctx.request.body._id }, ctx.request.body); let new2 await Model.update(ctx.request.body, { where: { id: ctx.request.body.id } }); ctx.body new2})//设置删除delete接口router.post(/delete, async function (ctx, next) { console.log(ctx.request.body) // 删除所有名为 Jane 的人 await Model.destroy({ where: { id: ctx.request.body.id } }); ctx.body shibai })// //每次启动server刷新数据库// (async (){// await sequelize.sync({ force: true });// })()module.exports router12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273744.2在app.js里面添加路由//引入刚刚创建的new2路由const new2 require(./routes/new2)12//使用我们的路由app.use(new2.routes(),new2.allowedMethods())124.3启动项目在数据库中查看5.测试5.1使用浏览器查看输入urlhttp://localhost:3000/new25.2.使用postman测试接口测试find接口(因为我们写的find方法使用的post方法所以记得将get换成post)http://localhost:3000/new2/find1测试get接口展示一下最后的目录文章来源: blog.csdn.net作者ky_xin版权归原作者所有如需转载请联系作者。原文链接blog.csdn.net/weixin_44858959/article/details/111691928