简体   繁体   中英

how to use mysql in node.js all app files?

Hello mates, first, I hope everyone is in good health.

Sorry if this question is a little confuse, but i'm trying creating a API/webservice that works with MySql, so I have

in root (/) the file bd.js with the connection

bd.js:

var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : '127.0.0.1',
    user     : 'root',
    password : '',
    database : 'skey-9'
});

connection.connect(function(err) {
    if (err) throw err;
});

module.exports = connection;

then i add to /app.js and have the routes of diferente directories

app.js :

const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const db = require('./bd');

const productRoutes = require('./api/routes/products');
const orderRoutes = require('./api/routes/order');

app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json());

//Routes 
app.use('/products', productRoutes);
app.use('/orders', orderRoutes);

in the final i'm trying to query a Select in /routes/products.js

products.js:

const express = require('express');
const router = express.Router();
var db = require('./../bd');


con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});


con.query('SELECT * FROM teste', (err,rows) => {
    if(err) throw err;
  
    console.log('Data received from Db:');
    console.log(rows);
  });


router.get('/', (req, res, next) => {
        res.status(200).json({
        message: 'handling GET requests to / products',
        query: rows
        
    });
});



module.exports = router;

but i'm getting a error i already tried to "playing" with the bd files.

the error:

C:\NODE\node-rest\server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
    at Function.Module._load (internal/modules/cjs/loader.js:840:27)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\NODE\node-rest\api\routes\products.js:3:10)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1019:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\NODE\\node-rest\\api\\routes\\products.js',
    'C:\\NODE\\node-rest\\app.js',
    'C:\\NODE\\node-rest\\server.js'
  ]

My question is how can i do a "global" mysql connection to all the app I'm creating.

soo i have 2 subpath's, and for some reason i had to change

 var db = require('./../bd'); " 

to

var db = require('..\\..\\bd'); " 

forget this dont resolve my problem, we have to run a connection to all the router's?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM