Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add and query database data by nodejs

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to add nodejs and query database data, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Add and query in database based on node.js

Train of thought

Create a project serverAPI

Initialize the project folder

Npm init-- y

Installation package

Npm i express mysql

Restfulf style

Testing with Postman software

[recommended: "nodejs tutorial"]

Project structure diagram

Realize

The sql.js file code is as follows:

/ / 1. Load msyqlvar mysql = require ('mysql'); / / 2. Create a connection var connection = mysql.createConnection ({host: 'localhost', / / the address of the database server you want to connect to: port: 3306 user / port number:' root', / / the user name required to connect to the database server password: 'root' / / password required to connect to the database server database: 'yanyan' / / the name of the database you want to connect to}) / / 3. Connect to the database connection.connect ((err) = > {/ / if there is an error object, the connection failed if (err) return console.log ('database connection failed') / / No error object indicates that the connection succeeded console.log ('mysql database connection succeeded')}; module.exports = connection

Server.js file reference code

Const express = require ("express"); const app = express (); const connection = require (". / utils/sql"); app.use (express.urlencoded ()); / / add data interface app.post ("/ api/student", (req, res) = > {console.log (req.body); / / receive common key pair parameter const {name, sex, age} = req.body / add to the database const sql = `insert into Students (name,sex,age) value ('${name}','${sex}', ${age}) `; / / console.log ("sql to be executed", sql); / / data accepted by result connection.query (sql, (err, result) = > {if (err) {console.log (err); res.json ({msg: "add failed", code: 0}) } else {console.log (result); res.json ({msg: "added successfully", code: 1});}}); / / get the data interface app.get ("/ api/student", (req, res) = > {const sql = `select * from Students `; connection.query (sql, (err, result) = > {if (err) {console.log (err)) Res.json ({msg: "acquisition failed", code: 0});} else {console.log (result); res.json ({msg: "get success", code: 0, data: result}); app.listen (3000, () = > {console.log ("API server starts, port number is 3000");})

Running result

Sql database

Postman test

Console output result

Optimization Scheme using routing Middleware

Train of thought

Create a project

Initialize the project folder

Npm init-- y

Installation package

Npm i express mysql

Restfulf style

Testing with Postman software

Project structure diagram

Realize

Sql.js file

/ / 1. Load mysqlvar mysql = require (". / node_modules/mysql"); / / 2. Create a connection var connection = mysql.createConnection ({host: "localhost", / / the address of the database server you want to connect to: port: 3306, / / the port number user: "root", / / the user name needed to connect to the database server password: "root", / / the password required to connect to the database server: "yanyan", / / the name of the database you want to connect to}); / / 3. Connect to the database connection.connect ((err) = > {/ / if there is an error object, if (err) return console.log ("database connection failure"); / / No error object indicates that the connection was successful console.log ("mysql database connection succeeded");}); module.exports = connection

Get.js file

Const connection = require (". / sql"); const express = require (". / node_modules/express"); const router = express.Router (); router.use (express.urlencoded ()); / / data acquisition API router.get ("/ api/student", (req, res) = > {const sql = `select * from Students`; connection.query (sql, (err, result) = > {if (err) {console.log (err)) Res.json ({msg: "acquisition failed", code: 0});} else {console.log (result); res.json ({msg: "get success", code: 0, data: result}); module.exports = router

Post.js file

Const connection = require (". / sql"); const express = require (". / node_modules/express"); const router = express.Router (); router.use (express.urlencoded ()); / / add data interface router.post ("/ api/student", (req, res) = > {/ / console.log (req.body); / / receive common key pair parameter const {name, sex, age} = req.body / add to the database const sql = `insert into Students (name,sex,age) values ('${name}','${sex}', ${age}) `; / / console.log ("sql to be executed", sql); / / data accepted by result connection.query (sql, (err, data) = > {if (err) {console.log (err); res.json ({msg: "add failed", code: 0}) } else {console.log (data); res.json ({msg: "added successfully", code: 1});}});}); module.exports = router

Server-pro.js file

Const get = require (". / utils/get"); const post = require (". / utils/post"); const express = require (". / node_modules/express"); const app = express (); app.use ("/ utils/get", get); app.use ("/ utils/post", post); app.listen (3000, () = > {console.log ("Interface server starts, port number is 3000");}) The above is all the contents of the article "how to add and query database data in nodejs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report