“Cómo conectar MySQL usando Node JS Stack” Código de respuesta

Cómo conectar MySQL usando 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;
Average Alligator

Cómo conectar MySQL usando 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);
Average Alligator

Cómo conectar MySQL usando 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}\nfields: ${fields}`);
  });

});

app.listen(PORT);
Average Alligator

Respuestas similares a “Cómo conectar MySQL usando Node JS Stack”

Preguntas similares a “Cómo conectar MySQL usando Node JS Stack”

Más respuestas relacionadas con “Cómo conectar MySQL usando Node JS Stack” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código