Rasul Imanov
Rasul Imanov

Reputation: 45

How do I parse URL query into mysql database? using node.js and express

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) => {

})

Upvotes: 0

Views: 733

Answers (1)

Mestre San
Mestre San

Reputation: 1925

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)
    })
})

Upvotes: 1

Related Questions