Answers for "node js mysql example"

SQL
4

how to manage a db connection in javascript

//put these lines in a seperate file
const mysql = require('mysql2');

const connection = mysql.createPool({
    host: "localhost",
    user: "",
    password: "",
    database: ""
    // here you can set connection limits and so on
});

module.exports = connection;

//put these on destination page
const connection = require('../util/connection');

async function getAll() {
    const sql = "SELECT * FROM tableName";
    const [rows] = await connection.promise().query(sql);
    return rows;
} 
exports.getAll = getAll;
Posted by: Guest on May-09-2020
1

rest api node js with mysql

const express = require('express'),
  app = express(),
  bodyParser = require('body-parser');
  port = process.env.PORT || 3000;


const mysql = require('mysql');
// connection configurations
const mc = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'mydb'
});
 
// connect to database
mc.connect();

app.listen(port);

console.log('API server started on: ' + port);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var routes = require('./app/routes/approutes'); //importing route
routes(app); //register the route
Posted by: Guest on July-10-2020
1

js read mysql database

// Use https://github.com/JackChilds/jsmysqldb

// Then once it's setup you can just do this:

// Config that will be sent to server
const config = {
  "table" : "customers",
  "limit" : "50",
  "offset" : "0"
};

// Use the _POST_REQUEST function to send data to the server and get a response
_POST_REQUEST("link-to-server/database.php", 'config=' + JSON.stringify(config), (response) => {
  // Convert the response into an array of rows:
  var data = JSON.parse(response);
  // Now do what you want with data
  // data[row index][column name]
});
Posted by: Guest on October-19-2020

Code answers related to "SQL"

Browse Popular Code Answers by Language