Best approach to disentangle nowjs code from your application (written in coffee/js)

Is it advisable to separate my nowjs code from the main app file?

everyone = require("now").initialize app, { socketio: { transports: ['xhr-polling', 'jsonp-polling'] } }

everyone.now.distribute_event = (event, day) ->
  everyone.now.receive_event @now.name, event, day

I believe that defining the functions in a separate file like routes would be a good idea. Would the process look something like this?

nowtes = require('./nowtes')

everyone = require("now").initialize app, { socketio: { transports: ['xhr-polling', 'jsonp-polling'] } }

everyone.now.distribute_event = nowtes.distribute_event

Answer №1

After some trial and error, I managed to get things running smoothly with the following approach:

let globalNow = require("now")
let globalEveryone = now.initialize(app, {
  socketio: {
    transports: ['xhr-polling', 'jsonp-polling']
  }
})

let nowtes = require("./nowtes")

globalEveryone.now.distributeEvent = nowtes.distributeEvent

Implemented in nowtes/index.coffee:

exports.distributeEvent = (activity, day) => {
  everyone.now.receiveEvent(@now.name, activity, day)
}

The code structure now appears much more organized and manageable.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am interested in utilizing node.js to input data into a mongodb Database

Currently, I am working on writing data to a JSON file and retrieving it back to an HTML page for display. Now, I want to achieve the same functionality with a MongoDB database. I have made some attempts, but unfortunately, it is not functioning as expecte ...

Is there a restriction on the number of strings allowed in minimist?

Here is the output received from the code provided below. Question input and i are both true as intended, but why aren't project and p? They are defined in exactly the same way as input and i. $ bin/test --input -p { _: [], update: fa ...

Encountering an issue such as receiving the 'Error eacces mkdir' message when trying to execute the 'npm install -g create-react-app' command

Encountered an error while trying to install reactjs on Ubuntu using the npm install -g create-react-app command. The error message is as follows. Can someone please assist me in resolving this issue? This is the error I am receiving: npm ERR! Error: EAC ...

The module in Node.js is unable to be loaded

Dealing with a common problem here. Despite trying to reinstall npm, deleting node_modules files and package-lock.json, the issue persists. The console output is as follows: node:internal/modules/cjs/loader:1080 throw err; ^ Error: Cannot find module &apo ...

Prevent EACCES issues with npm/node when using Ubuntu?

Setting up the system environment $ lsb_release -a 2> /dev/null | grep Desc Description: Ubuntu 14.04.1 LTS $ sudo apt-get install build-essential $ sudo add-apt-repository ppa:chris-lea/node.js && sudo apt-get update $ sudo apt-get install ...

My server keeps crashing due to an Express.js API call

I'm completely new to express.js and API calls, and I'm stuck trying to figure out why my server keeps crashing. It works fine the first time, rendering the page successfully, but then crashes with the error: TypeError: Cannot read property &apo ...

Encountered a failure while attempting to substitute environment variables in configuration files with the help of Bash and

Trying to integrate a private NPM module into my application is proving to be quite challenging. I need to configure the NPM access tokens correctly so that third-party tools like Heroku and CI can access and install the module. In my ~/.bash_profile, I h ...

Learn how to create a button that will only submit a value when clicked using Node.js and EJS

Currently in my ejs file, I have a button that sends a value to app.js instantly when the program runs. However, I want it to only submit the value when clicked by the user. <% notesArray.forEach((note,i) =>{ %> <div class="note"> ...

Position of JSON data response is inaccurate

Currently, I have a function that calls an API from the server in the following manner: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-ww ...

I've been attempting to develop a React application, but I consistently encounter the following error: "npm ERR! cb() function was never invoked!"

Here is the issue at hand: HP@DESKTOP-1HP83V8 MINGW64 ~/Desktop/Web-Development (master) $ npx create-react-app my-app A new React app is being created in C:\Users\HP\Desktop\Web-Development\my-app. Packages are being installed. ...

Running a child process within an express js route

I have a problem where I am trying to execute a child process within my Express JS route and then return a JSON message with the output of this process as the message value. However, I am currently experiencing an issue where the stdout value is coming bac ...

Error starting the Heroku web service: start script failed

Encountering the following issue: Failed at the [email protected] start script. while trying to deploy an express + react application. The problem seems to be related to concurrently on Heroku. https://i.stack.imgur.com/BEuZL.png Any suggestions ...

Ensure to first close the existing global connection before attempting to establish a new one in your Node.js Post WebApi application

Currently, I am in the process of creating a small post WebAPI using node.js to collect user data such as names and numbers. However, when I have an excessive number of users accessing this web API, I encounter the following error message: "node.js Global ...

When using React.JS / Next.JS with @material-ui/core/styles, you may encounter the `ERR_UNSUPPORTED_DIR_IMPORT` error

When importing { makeStyles } from '@material-ui/core/styles', an error is encountered:</p> <pre><code>- information Collection page data ..Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/usr/src/sellerportal/node_mo ...

"Uncaught Error: Unable to retrieve null properties" encountered while utilizing regex match in cheerio web scraping

Extracting text from brackets in HTML using regex: <dl class="ooa-1o0axny ev7e6t84"> <dd class="ooa-16w655c ev7e6t83"> <p class="ooa-gmxnzj">Cekcyn (Kujawsko-pomorskie)</p> </dd> <dd class="ooa-16w655c ev7e6t ...

Discover the step-by-step process of combining an array with JSON to create the desired outcome

I am working with a JSON array that looks like this: var row={ shopId: 3, shopName: '1', address: 'abc', contactNumber: 1234 } Alongside, I have another array: var data= [ { imageId: 1, shopId: 3, imageUrl: 'aaa' }, ...

Should we consider packaging the npm dependencies code along with our code as a best practice?

What is the best way to handle npm dependencies in our code bundle? If it's preferable to include the npm dependency code in our bundle, does it make sense to add it as a separate module or package? If not, how can I prevent bundling my dependencie ...

A discrepancy in Node.js versioning caused Electron to be compiled with an incompatible version

I am currently in the process of developing an Electron application intended to operate on Ubuntu 20.xx Linux and a Raspberry Pi (which is running Raspbian with arch=armv7l). During development, I encountered the following error: ...was compiled against a ...

Prevent unauthorized entry to nodejs express endpoints

I am facing a challenge with the "internal" routes within my application: app.get('/myroute', function(req, res) { res.status(200).send('Hello!'); }); The issue is that this route can also be accessed from external sources: curl http ...

Sequelize One to Many Relationship Update not functioning

This is how the connection between my two models is established: DebitInvoice.hasMany(DebitInvoiceProduct, { onDelete: 'CASCADE', onUpdate: 'CASCADE', foreignKey: 'invoice_serial' }); DebitInvoiceProduct.belongsTo( ...