使用express和nodejs插入mysql

编程入门 行业动态 更新时间:2024-10-11 01:14:00

使用<a href=https://www.elefans.com/category/jswz/34/1771445.html style=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

本文发布于:2024-05-07 15:33:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:express   nodejs   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!