node mysql增删改查入门

68 阅读1分钟

安装mysql

(blog.csdn.net/qq_45956730…)

入门

先记下安装完使用mysql的遇到的问题,数据库连接报错: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

原因:mysql8.0以上加密方式,Node sql还不支持。

解决:

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.27 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.08 sec)

项目启动和路由文件:

serve.js: `const express = require('express'); const cors = require('cors');

const app = express();

var corsOptions = { origin: "http://localhost:3000" };

app.use(cors(corsOptions));

// parse requests of content-type - application/json app.use(express.json());/* bodyParser.json() is deprecated */

// parse requests of content-type - application/x-www-form-urlencoded app.use(express.urlencoded({ extended: true })); /* bodyParser.urlencoded() is deprecated */

app.get('/',(req,res) => { res.json({message: 'welcon to application'}) })

require("./app/routes/tutorial.routes.js")(app);

// set port, listen for requests const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(Server is running on port ${PORT}.); });`

./app/routes/tutorial.routes.js:

`module.exports = app => { const tutorials = require("../controllers/tutorial.controller.js");

var router = require("express").Router();

// Create a new Tutorial
router.post("/", tutorials.create);

// Retrieve all Tutorials
router.get("/", tutorials.findAll);

// Retrieve all published Tutorials
router.get("/published", tutorials.findAllPublished);

// Retrieve a single Tutorial with id
router.get("/:id", tutorials.findOne);

// Update a Tutorial with id
router.put("/:id", tutorials.update);

// Delete a Tutorial with id
router.delete("/:id", tutorials.delete);

// Delete all Tutorials
router.delete("/", tutorials.deleteAll);

app.use('/api/tutorials', router);

};`

最后附上node项目地址

项目地址