Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8448049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:19:29+00:00 2026-06-10T10:19:29+00:00

I have imported mysql module in my app.js : var mysql = require(‘mysql’); Then

  • 0

I have imported mysql module in my app.js:

var mysql = require('mysql');

Then I set mysql configuration:

// Database configuration

var HOST            = 'localhost';
var PORT            = 3306;
var MYSQL_USER      = 'root';
var MYSQL_PASS      = 'pass';
var DATABASE        = 'ruchevits_website';

Created mysql client object:

// Create database client

var db = mysql.createClient({
    host:       HOST,
    port:       PORT,
    user:       MYSQL_USER,
    password:   MYSQL_PASS
});

And selected my database:

// Use database

db.query('use ' + DATABASE);

So, now I can execute queries like:

db.query('select id, firstname, lastname from mytable', function(err, result, fields){
    if (err)
    {
        throw err;
    }
    else
    {
        console.log('Selected people:');
        for (var i in result)
        {
            var people = result[i];
            console.log(people.firstname + ' ' + people.lastname);
        }
    }
});

But it is inconvenient to execute all queries in app.js.

I want to execute them in specific routes files in the section:

exports.index = function(req, res)
{
    // MySQL query

    res.render('resume', {
        title: 'Resume'
    });
};

What is the correct way to do that?

I need to use db object not in app.js file. Actually, the question is how to pass it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:19:31+00:00Added an answer on June 10, 2026 at 10:19 am

    Create a module and export a function that returns the db object.

    in dbConnection.js

    var mysql = require('mysql');
    var db = null;
    module.exports = function () {
        if(!db) {
            db = mysql.createClient({
                host:       HOST,
                port:       PORT,
                user:       MYSQL_USER,
                password:   MYSQL_PASS
            });
        };
        return db;
    };
    

    use it with var db = require('./dbConnection')(); where ever you need it. Also check out Node.js module caching feature http://nodejs.org/api/modules.html#modules_caching.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just imported a huge MySQL database. Most fields are latin1_swedish_ci, and they
i have downloaded whole database from geonames.org and imported all countries into mysql. i
I have to migrate database from mysql to ms sql server 2005.I imported table
I have a CSV file to be imported to MYSQL database. The problem is
I have a mysql database set as utf-8, and csv data set as utf-8,
I have imported a mysql from a web app into my iOS app. This
I have imported a database over to MySQL in phpMyAdmin. I am performing my
I have exported a database and then imported it into a new database. Then
I exported and then imported a wordpress mysql database from one server to another.
I have imported the data into an database. Patient(pt_id,pt_name,pt_type,pt_loc) Now , I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.