Node js express how to create new datatable once another one is full

强颜欢笑 提交于 2020-05-17 08:49:29

问题


I'm working on a project right now that will populate a MySQL database from a script I will be running on port 3000. The script looks something like this:

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"

And right now my express/NodeJS code looks like this:

const express = require('express');
//const session = require('express-session');
const mysql = require('mysql');

let app = express();


var pool = mysql.createPool( {
        host: 'localhost',
        user: 'root',
        password: 'password',
        database: 'cars',
        connectionLimit: 10,
        multipleStatements : true
});


app.get('/register', function(req, res) {
    pool.query("INSERT INTO cars (`name`, `width`, `time`) VALUES (?,?,?)", [req.query.name, req.query.width, req.query.time]);
    console.log(req.query.name);
    console.log(req.query.width);
    console.log(req.query.time);
    res.redirect('localhost:3000/wheels');
});


app.get('/wheels', function (req, res) {
        pool.query("UPDATE cars SET left1 = ?, right1 = ?, time = ?",
        [req.query.left, req.query.right, req.query.time])
    console.log(req.query.left);
    console.log(req.query.right);
    console.log(req.query.time);
    res.redirect('localhost:3000/echo');
});

app.get('/echo', function (req, res) {

    pool.query("UPDATE cars SET dist =?, time = ?",
        [req.query.dist, req.query.time])
        console.log(req.query.dist);
        console.log(req.query.time);
        res.redirect('localhost:3000/line');
});


app.get('/line', function (req, res) {

    pool.query("UPDATE cars SET l1 = ?, l2 = ?, l3 = ?, time =?",
        [req.query.l1, req.query.l2, req.query.l3, req.query.time])
    console.log(req.query.l1);
    console.log(req.query.l2);
    console.log(req.query.l3);
    console.log(req.query.time);
    res.redirect('localhost:3000/other');
});

app.get('/other', function(req, res) {

    pool.query("UPDATE cars SET ir = ?, time =?",
        [req.query.ir, req.query.time])
    console.log(req.query.ir);
    console.log(req.query.time);
    res.redirect('localhost:3000/wheels');
});


app.listen(3000, function(req, res) {
        console.log('Express JS ready for port 3000');
});

Right now it will create a new data table and constantly update it from the script.. But what I would like to do, is when one datatable is filled (like at the end of /other) we create a new datatable that will hold in the next round of /register, /wheels, /echo, /line, and /other. For this project I'm not worried about security or anything, just want to mess around with databases. Eventually I want to display the database on html but right now I just need to figure out how to populate my MySql database cars first. Any help would be great!

来源:https://stackoverflow.com/questions/61842957/node-js-express-how-to-create-new-datatable-once-another-one-is-full

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!