dome地址:https://gitee.com/longjun168/node_test.git
.首先安装express,安装步骤
npm install -g express-generator #需先安装express-generator
npm install -g express
express --version #最后验证是否安装成功
1.创建mysql数据库名称oa 表名 user
表结构如下:
2.项目根目录下创建config目录,config目录内新建db.js,来实现基本的数据库连接
var mysql = require("mysql"); //引入mysql模块
var pool = mysql.createPool({ //创建mysql实例
host: "localhost", //ip地址
port:'3306', //端口号
user:'root', //数据库用户
password: "admin", //数据库密码
database: "oa" //数据库名
});
function query(sql, callback) {
pool.getConnection(function(err, connection) {
connection.query(sql, function(err, rows) {
callback(err, rows);
connection.release();
});
});
}
exports.query = query;
数据表增删改查的业务代码实现,demo中暂时放到routes下的user.js中(demo的项目目录结构可能不大合理,暂且不纠结于此)
var express = require('express');
var router = express.Router();
var db = require("../config/db");
/**
* 查询列表页
*/
router.get("/", function(req, res, next) {
db.query("select * from user", function(err, rows) {
if (err) {
/** res.render是渲染模板语法,如果没绑定静态模板测试时会报错,
* 可直接删除下面语法,可换成res.send(rows)返回给前端
*/
res.render("users", {
title: "用户列表",
datas: []
});
} else {
res.render("users", {
title: "用户列表",
datas: rows
});
}
});
});
/**
* 添加用户
*/
router.get("/add", function(req, res, next) {
res.render("add");
});
router.post("/add", function(req, res, next) {
var name = req.body.name;
var age = req.body.age;
db.query("insert into user(name,age) values('" + name + "','" + age + "')", function(err, rows) {
if (err) {
res.send("新增失败" + err);
} else {
res.redirect("/users");
}
});
});
/**
* 删除用户
*/
router.get("/del/:id", function(req, res) {
var id = req.params.id;
db.query("delete from user where id = " + id, function(err, rows) {
if (err) {
res.send("删除失败" + err);
} else {
res.redirect("/users");
}
});
});
/**
* 修改
*/
router.get("/toUpdate/:id", function(req, res, next) {
var id = req.params.id;
var sql = "select * from user where id = " + id;
console.log(sql);
db.query(sql, function(err, rows) {
if (err) {
res.send("修改页面跳转失败");
} else {
res.render("update", {
datas: rows
});
}
});
});
router.post("/update", function(req, res, next) {
var id = req.body.id;
var name = req.body.name;
var age = req.body.age;
var sql = "update user set name = '" + name + "',age = '" + age + "' where id = " + id;
console.log(sql);
db.query(sql, function(err, rows) {
if (err) {
res.send("修改失败 " + err);
} else {
res.redirect("/users");
}
});
});
/**
* 查询
*/
router.post("/search", function(req, res, next) {
var name = req.body.s_name;
var age = req.body.s_age;
var sql = "select * from user";
if (name) {
sql += " where name = '" + name + "'";
}
//if(age){
// sql += " and age = '" + age + "'";
//}
sql.replace("and", "where");
db.query(sql, function(err, rows) {
if (err) {
res.send("查询失败: " + err);
} else {
res.render("users", {
title: "用户列表",
datas: rows,
s_name: name,
s_age: age
});
}
});
})
module.exports = router;
如何设置解决跨域问题
首先安装:npm i --save cors
接着在app.js中加入
app.use('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);//注意这里不能使用 *
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header("X-Powered-By",' 3.2.1');
res.header('Access-Control-Allow-Credentials', true); // 允许服务器端发送Cookie数据
res.header("Content-Type", "application/json;charset=utf-8");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');//设置方法
if (req.method == 'OPTIONS') {
res.sendStatus(200); // 在正常的请求之前,会发送一个验证,是否可以请求。
}
else {
next();
}
});