“Como conectar o MySQL usando o nó JS Stack” Respostas de código

Como conectar o MySQL usando o nó 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

Como conectar o MySQL usando o nó 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

Como conectar o MySQL usando o nó 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

Respostas semelhantes a “Como conectar o MySQL usando o nó JS Stack”

Perguntas semelhantes a “Como conectar o MySQL usando o nó JS Stack”

Mais respostas relacionadas para “Como conectar o MySQL usando o nó JS Stack” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código