[英]How to create a webservice using node.js to insert data into mysql?
I've a table in mysql that i need to be populated by calling a webservice. 我在mysql中有一个表,需要通过调用Web服务来填充。
The catch is, I would like to pass the data to be inserted into the database as parameters to the webservice. 问题是,我想将要插入到数据库中的数据作为参数传递给Web服务。
How would I do this? 我该怎么做?
Using the MySQL module 使用MySQL模块
http://npmjs.com/mysql http://npmjs.com/mysql
const mysql = require('mysql');
const express = require('express');
const app = express();
const connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'secret',
database: 'my_db'
});
connection.connect();
app.get('/', (req, res) => {
connection.query(`SELECT * FROM table`, (error, data) => {
if(error) {
console.log(error)
}else{
res.json(data)
}
});
});
app.post('/update/user/:id?', (req, res) => {
const id = req.params.id;
const username = req.body.username;
connection.query(`UPDATE MyGuests SET username='${username}' WHERE id=${id}`, (error, data) => {
if(error) {
console.log(error)
}else{
res.json(data)
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.