0

I have to store information such as name and width from the url query to my mysql database and the url query should look something like this

/register?name=XXXX&width=###.###

I guess I'm having trouble understanding the initial process of taking the inputs through my code that is sent through the URL query.

Here is my /register portion of my code. Any help is appreciated.

    const express = require('express');
    const mysql = require('mysql');
    const moment = require('moment');
    var cookieParser = require('cookie-parser');
    var Cookies = require('cookies');

    const db = mysql.createConnection({
      host     :     'localhost',
      user     :     'root',
      password :     'somepassword',
      database :     'somedatabase'
    })

const app = express();

app.use(express.cookieParser());

app.get('/register', (req, res) => {

})
  • Does this answer your question? [How to get a URL parameter in Express?](https://stackoverflow.com/questions/20089582/how-to-get-a-url-parameter-in-express) – danblack May 18 '20 at 01:46

1 Answers1

1

For that you will need the Performing Queries documentation from the mysql package and the request.query from express.

app.get('/register', (req, res) => {
  connection.query(
    'SELECT fieldA, fieldB, fieldC FROM yourTable WHERE author = ? and width = ?',
    req.query.author,
    req.query.width,
    function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
    })
})
Mestre San
  • 1,806
  • 15
  • 24