We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这年头,不会点node都不好意思说自己是前端。ok,下面显示的是一个添加用户和显示所有用户的一个简单demo。
// config.js module.exports = { // MySQL数据库联接配置 mysql: { host: '127.0.0.1', user: 'root', password: '', database:'test', // 数据库名字 port: 3306 } };
// routers/users.js // 增加用户 router.get('/addUser', function (req, res, next) { user.add(req, res, next); }); // 返回所有用户 router.get('/allUser', function (req, res, next) { user.all(req, res, next); });
// 定义文件 controller/userSqlMapping.js var user = { insert:'INSERT INTO mytable(id, name) VALUES(?,?)', queryAll: 'select * from mytable' }; module.exports = user;
// controller/user.js "use strict"; // 实现与MySQL交互 var mysql = require('mysql'); var config = require('../common/config.js'); var $sql = require('./userSqlMapping'); module.exports = { // 添加用户 add: function (req, res, next) { var param = req.query || req.params; var connection = mysql.createConnection(config.mysql); connection.connect(); connection.query($sql.insert, [param.id, param.name], function (err, rows, fields) { if (err) { res.json({ code: '1', msg: '操作失败' }); } res.json({ code: 200, msg: "增加成功" }); }); connection.end(); }, // 返回用户信息 all: function (req, res, next) { var connection = mysql.createConnection(config.mysql); connection.connect(); connection.query($sql.queryAll, function (err, rows, fields) { if (err) { res.json({ code: '1', msg: '操作失败' }); } res.json({ code: 200, msg: rows }); }); connection.end(); } };
添加用户
查询用户
查看数据库
参考资料: http://www.alloyteam.com/2015/03/sexpressmysql/
The text was updated successfully, but these errors were encountered:
No branches or pull requests
问题
这年头,不会点node都不好意思说自己是前端。ok,下面显示的是一个添加用户和显示所有用户的一个简单demo。
准备条件
使用方法
1. 把mysql的配置写在配置文件中
2. 添加路由
3. 定义sql查询语句
4. 编写控制器中间件
5.演示结果
添加用户
查询用户
查看数据库
问题
参考资料:
http://www.alloyteam.com/2015/03/sexpressmysql/
The text was updated successfully, but these errors were encountered: