express和nodejs插入mysql"/>
使用express和nodejs插入mysql
我想知道用从Express和Node js从脚本中提取的查询填充MySQL数据库的最佳途径是什么。>>
我在端口3000上运行的脚本如下:
curl localhost:3000/register?name=bob\&width=13.970000\&time=0 curl localhost:3000/wheels?left=0.000000\&right=0.000000\&time=0 --cookie "USER=bob" curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=bob" curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=bob" curl localhost:3000/other?ir=0\&time=30 --cookie "USER=bob" curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=100 --cookie "USER=bob" curl localhost:3000/echo?dist=9.220000\&time=110 --cookie "USER=bob" curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=120 --cookie "USER=bob" curl localhost:3000/other?ir=0\&time=130 --cookie "USER=bob" curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=200 --cookie "USER=bob" curl localhost:3000/echo?dist=9.220000\&time=210 --cookie "USER=bob" curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=220 --cookie "USER=bob"
我的app.js文件看起来像这样:
const express = require('express'); const cookieParser = require('cookie-parser'); const mysql = require('mysql'); let app = express() app.use(cookieParser()); var pool = mysql.createPool( { host: 'localhost', user: 'root', password: 'root', database: 'cars', connectionLimit: 10, multipleStatements : true }); app.get('/register', function(req, res) { mysql.pool.query("INSERT INTO todo (`name`) VALUES (?)", [name], function( err, results ) { if (err) throw err response.send(results); }) }); app.listen(3000, function(req, res) { console.log('Express JS ready for port 3000'); });
我的数据库是在phpMyAdmin中设置的,看起来像这样:My database using phpMyAdmin
当我在端口3000上运行脚本时,我一直收到的错误是:
TypeError: Cannot read property 'query' of undefined
最终,我想使用/ wheels,/ line,/ echo等填充MySQL数据库。但是,我对它的了解越深,得到的类似错误就越多。任何人都可以看到我可能出了问题的地方,或者可以指出我可能在哪里学习如何正确地执行此类操作的正确选择?谢谢
我想知道用从Express和Node js从脚本中提取的查询填充MySQL数据库的最佳途径是什么。我在端口3000上运行的脚本如下所示:curl ...
回答如下:// file: server.js
const express = require('express');
const db = require('./db');
const app = express();
// you may wish to reconsider the use of .get at some stage
// what happens when you want to retrieve an item?
app.get('/models', (req, res) => {
// learn about validation when you're comfortable with basics
// this is especially important for public facing apps
if (!req.query.manufacturer || !req.query.type || !req.query.cost) {
return res.status(400).send('wrong input!'); // super helpful error messages
}
const public = req.query.public || true; // set defaults
const { manufacturer, type, cost } = req.query;
// when you're more comfortable, learn about abstractions + Service pattern
// removing DB queries from route handlers, pushing the responsibility to a Service.
db.query(
'INSERT INTO car_models(manufacturer,type,cost,public) VALUES (?,?,?,?)',
// learn about SQL injection, I **hope** the mysql package author considered it.
[manufacturer, type, cost, public],
(err, results) => {
if (err) {
console.error(err);
return res.status(500).send('oh no');
}
return res.status(201).send(`Created ID: ${results.insertId}`);
}
);
});
app.listen(3000, () =>
console.log('service started... accepting requests on http://localhost:3000')
);
更多推荐
使用express和nodejs插入mysql
发布评论