Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to connect mysql using node js stack

var mysql = require('./node_modules/mysql');
    var connection = mysql.createConnection({
            host: '127.0.0.1',
            user: 'root',
            password: '',
            database: 'nodeapp'
        });
    connection.connect(function (err) {
        if (err)
            throw err;
    });
    module.exports = connection;
Comment

how to connect mysql using node js stack

var express = require('./lib/express');
    var app = express();
    var bodyParser = require('body-parser')
    var db = require('/db');
     app.get('/', function (req, res) {
        res.sendFile('/NodeProj/views/' + 'index.html');
    });
    /** bodyParser.urlencoded(options)
     * Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
     * and exposes the resulting object (containing the keys and values) on req.body
     */
    app.use(bodyParser.urlencoded({
        extended: true
    }));

    /**bodyParser.json(options)
     * Parses the text as JSON and exposes the resulting object on req.body.
     */
    app.use(bodyParser.json());

    app.post('/process_form', function (req, res) {
        var response = {
            "firstname": req.body.fst_name,
            "email": req.body.fst_email,
            "password": req.body.fst_password
        };
        var query = connection.query('INSERT INTO register SET?',response,function(err,result){
            if(err) throw err;
            if(result) console.log(result);
        });
        res.end(JSON.stringify(response));
    });

    app.listen(8081);
Comment

how to connect mysql using node js stack

const mysql = require('mysql');
const express = require('express');
const app = express();
const PORT = // define the PORT for your host

var connection = mysql.createConnection({
    host: 'localhost',
    user: '<MY_USER_NAME>',
    password: '<MY_PASSWORD>',
    database: '<DB_NAME>'
});

connection.connect(function(err) {
  if (err) console.error(err);
  console.log("Connected!");
});

app.get('/db-test', (req, res, next) => {

  var id = // fill in a user_id that you know exists
  var sql = `SELECT * FROM users WHERE id NOT IN ('${id}') `;

  console.log(sql); // confirm you are sending the sql request you believe you should be sending

  connection.query(sql, function (err, results, fields) {
      if (err) console.error(err);
      console.log(`results: ${results}
fields: ${fields}`);
  });

});

app.listen(PORT);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js function to wrap an element 
Javascript :: react select and react hook form 
Javascript :: javascript round to 7 decimal places 
Javascript :: what is template engine in express 
Javascript :: password meter 
Javascript :: disable button in angular 
Javascript :: class function 
Javascript :: innertext javascript 
Javascript :: react google maps 
Javascript :: json parse returns object 
Javascript :: textbox in javascript 
Javascript :: react hooks component re render when button press 
Javascript :: mongoose search in multiple fields 
Javascript :: aggregate mongodb 
Javascript :: jquery select all checkboxes except disabled 
Javascript :: how to get updated data-value in jquery 
Javascript :: get last character of string javascript 
Javascript :: get last index of array of objects javascript 
Javascript :: delete item from array vuejs 
Javascript :: node.js log to file 
Javascript :: observable filter angular 8 
Javascript :: usecontext hook react 
Javascript :: index of row jquery 
Javascript :: list keys json python 
Javascript :: next auth session callback 
Javascript :: node get current user 
Javascript :: remove object from array by value javascript 
Javascript :: for loop in shopify liquid template 
Javascript :: xlsx js 
Javascript :: python convert json to csv 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =