1. var express = require('express');
    2. var app = express();
    3. var mysql = require('mysql');
    4. var bodyParser = require('body-parser');//解析,用req.body获取post参数
    5. app.use(bodyParser.json());
    6. app.use(bodyParser.urlencoded({ extended: false }));
    7. //设置跨域访问
    8. app.all('*', function (req, res, next) {
    9. res.header("Access-Control-Allow-Origin", "*");
    10. res.header("Access-Control-Allow-Headers", "X-Requested-With");
    11. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    12. res.header("X-Powered-By", ' 3.2.1');
    13. res.header("Content-Type", "application/json;charset=utf-8");
    14. next();
    15. });
    16. //连接数据库
    17. var connection = mysql.createConnection({
    18. host: 'localhost',//主机
    19. user: 'root',//mysql认证的用户名
    20. password: '123456',//mysql用户密码
    21. database: 'test',//数据库名
    22. port: '3306'//端口号
    23. });
    24. connection.connect();
    25. var questions = [
    26. {
    27. data: 213,
    28. num: 444,
    29. age: 12
    30. }
    31. ];
    32. //列表
    33. app.get('/list', function (req, res) {
    34. var sql = 'SELECT * FROM hello';
    35. connection.query(sql, function (err, result) {
    36. if (err) {
    37. console.log('[SELECT ERROR] - ', err.message);
    38. return;
    39. }
    40. res.json(result)
    41. });
    42. });
    43. //新增插入
    44. app.get('/list_add', function (req, res) {
    45. var addSql = 'INSERT INTO hello(name) VALUES(?)';
    46. var addSqlParams = req.query.name;
    47. connection.query(addSql, addSqlParams, function (err, result) {
    48. if (err) {
    49. console.log('[INSERT ERROR] - ', err.message);
    50. return;
    51. }
    52. res.json({ success: '添加成功' })
    53. });
    54. });
    55. //修改
    56. app.get('/list_edit', function (req, res) {
    57. var modSql = 'UPDATE hello SET name = ? WHERE Id = ?';
    58. var modSqlParams = [req.query.edit, req.query.id];
    59. //改
    60. connection.query(modSql, modSqlParams, function (err, result) {
    61. if (err) {
    62. console.log('[UPDATE ERROR] - ', err.message);
    63. return;
    64. }
    65. res.json({ success: '修改成功' })
    66. });
    67. });
    68. //删除
    69. app.get('/list_del', function (req, res) {
    70. var delSql = "DELETE FROM hello WHERE Id = ?"
    71. //删
    72. var delid = [req.query.id];
    73. connection.query(delSql, delid, function (err, result) {
    74. if (err) {
    75. console.log('[DELETE ERROR] - ', err.message);
    76. return;
    77. }
    78. res.json({ success: '删除成功' })
    79. });
    80. // console.log("主页 POST 请求");
    81. // res.send('Hello POST');
    82. })
    83. //登录接口
    84. app.get('/user', function (req, res) {
    85. res.status(200),
    86. res.json(questions)
    87. });
    88. //post接口 获取参数req.body
    89. app.post('/demo', function (req, res) {
    90. // console.log("主页 POST 请求");
    91. var delSql = "DELETE FROM hello WHERE Id = ?"
    92. var delid = [req.body.id];
    93. // console.log(req.body.id)
    94. connection.query(delSql, delid, function (err, result) {
    95. if (err) {
    96. console.log('[SELECT ERROR] - ', err.message);
    97. return;
    98. }
    99. res.json(result)
    100. });
    101. })
    102. //配置服务端口
    103. var server = app.listen(3000, function () {
    104. var host = server.address().address;
    105. var port = server.address().port;
    106. console.log('Example app listening at http://%s:%s', host, port);
    107. })