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 9006427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:24:31+00:00 2026-06-16T01:24:31+00:00

Background I’m making a simple web server using express.js on top of node.js. When

  • 0

Background

I’m making a simple web server using express.js on top of node.js. When I’m making the route handlers for my server I inevitably have to factor out some common functionalities. So I want to make a baseHandler which has all the common handler functionalities like DB connection, and when I’m writing other route handlers I want to ‘extend’ the baseHandler to conveniently obtain those common handler capabilities.

What I am trying

What are the ways of achieving it, and what is the correct way to do so? I am trying a pretty straight forward solution, I wrote baseHandler as a normal handler module, having a local mysql instance and exports the functions which manipulates the database. Code below:

// Mysql 
var _mysql = require('mysql');

// Mysql connection
exports.getDbHandle = function() {
    var mysql = _mysql.createConnection({
    host: xxx
    port: xxx
    user: xxx
    password: xxx
    database: xxx
});
return mysql;
};

Then in my route handlers I will simply require this module and use whatever functions it has. This seems to solve the problem, but I have a feeling that it is not the right way to do it. As I will have an instance of the baseHandler module in my route handler and it feels like a utility module, rather than a part of the route handler on its own.

Question

So what is the right way to extend modules in nodejs?

  • 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-16T01:24:32+00:00Added an answer on June 16, 2026 at 1:24 am

    Put the common functionality into middleware that you can plug into the routes that need that functionality instead of creating a class hierarchy. That forces you to cleanly decouple the functionality into independent components. The middleware can make things like database connections available to the routes by adding them as properties of the req request object.

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

Sidebar

Related Questions

Background: I am using node.js and express to create an API. I have implemented
Background : I am using nginx as a reverse proxy to my express server
Background I have written a simple WebGL puzzle game and I want to allow
Background: * using Corona SDK and the physic's functions here * have tracked down
Background: I'm in the process of creating a web service using ASP.NET 2.0. This
Background Use Ajax to fire an event to the web server when a list
Background Developing a simple web application (Eclipse + JBoss + Apache Tomcat) to generate
Background: our web services are company internal, but with a lot different systems using
Background I have an installation of VisualSVN on a server. under this, I have
Background: I have several builds running on a Windows Server 2003 R2 machine via

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.