“comment connecter mysql à l'aide de nœud js pile” Réponses codées

comment connecter mysql à l'aide de nœud js pile

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

comment connecter mysql à l'aide de nœud js pile

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

comment connecter mysql à l'aide de nœud js pile

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

Réponses similaires à “comment connecter mysql à l'aide de nœud js pile”

Questions similaires à “comment connecter mysql à l'aide de nœud js pile”

Plus de réponses similaires à “comment connecter mysql à l'aide de nœud js pile” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code