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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:25+00:00 2026-06-18T00:06:25+00:00

I am performing messaging via websockets between a HTML5 client and server running on

  • 0

I am performing messaging via websockets between a HTML5 client and server running on node.js. Naturally I chose JSON as the message format and as such created common javascript code, defining the various message content types and transformation operations. The javascript code is shared between both projects.

I created my web client as one git project and my server as another git project. Partly because I am using phonegap to build a webkit based client for various touch based environments. It’s also a nice separation of the various logic.

To share the common code I created a separate project for the common logic and used git’s subprojects to ‘import’ the code into the other two projects.

Now this works fine for the html5 based project, as I can just do the following to include the code:

<script src="common/js/comms.js" type="text/javascript"></script>

However with node I’ve had problems trying to get the code. to get the code, I’ve ended up doing the following:

var fs = require('fs');
eval(fs.readFileSync('./common/js/comms.js').toString());

While the approach I have taken works, I’ve noticed that it’s starting to get very messy when I have dependencies (as in, I need x.js, y.js and x.js if I want a.js), and I have to do it for every single node.js js file that wishes to use any of these entities.

I’m also not comfortable using the eval approach. I don’t have a security issue with it, though I would like to use strict mode and it’s my understanding that eval and strict mode go together like oil and water.

So my question is, what is the best method to include shared js files between html projects and node.js projects? I would prefer something that follows strict.

I should note that while there are several questions that are kinda around this topic, I could not find any that address the specific issues I’m raising. I should also add that I do not wish to ‘serve’ the files from the ‘server’. The HTML5 client is to be ‘standalone’.


To clarify, what I have in the ‘common js files’ is something like the following:

var Comms = function (options) {
   ...
}

In HTML5 I can then just reference is via new Comms(), which is what I desire to do in node.js as well.

  • 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-18T00:06:27+00:00Added an answer on June 18, 2026 at 12:06 am

    Have you researched how Node modules work? If you’re developing using this pattern, it’s fairly simple to use require('./common/js/comms') on the server while still including it on your client as well.

    This article should point you in the right direction: https://caolan.org/posts/writing_for_node_and_the_browser.html


    The following is the code that Tyler linked to in his comments below.

    The example (example.js):

    if(typeof exports == "undefined"){
        exports = this;
    }
    
    Example = function() {
        this.init();
    };
    
    Example.prototype = {
        init: function() {
             console.log('hello world');
        }
    };
    
    exports.Example = new Example();
    

    The node.js usage of example.js (app.js):

    example = require('./example');
    

    The html usage of example.js (index.html):

    <html>
        <head>
            <title></title>
        </head>
        <body>
            <script src="./example.js"></script>
        </body>
    

    The change by the OP was to assign exports.Example to Example instead of to an new instance. Thus the node.js logic can use the following:

    var Example = require('./example.js').Example;
    var foo = new Example();
    

    Thus the original question is solved.

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

Sidebar

Related Questions

I'm performing a trade study on (Java) Messaging & Queuing systems for an upcoming
After performing processing I want to calculate the percentage of white pixels between this
When performing some action on another user's repo, you will see the message Not
When performing an insert lets say from C# into a SQL Server table (using
My site has a messaging feature where one user may message another. The messages
When performing many disk operations, does multithreading help, hinder, or make no difference? For
While performing a conditional DELETE operation in one of my InnoDB tables, which apparently
While performing some upcoming maintenance, I'm going to have to redirect all site traffic
When performing many inserts into a database I would usually have code like this:
Occasionally when performing a rebase using the MercurialEclipse plugin my repository gets thrown into

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.