JavaScript Routing with Multiple Files

I tried to access api.js from Routes.js, but I encountered an issue stating that the function my_function_in_api is not defined. Here is my code, could you please help me identify where the problem lies:

Routes.js

var val = require('file name')

modules.exports = function(app){
app.get('/test_function',function(req,res){
val.my_function_in_api(req,res)})

api.js

module.exports = (function() {
return {
my_function_in_api: function(req,res) {
// do something}})

Answer №1

Expanding on Fischer's response, when exporting from api.js as a function, it is crucial to properly invoke the default function in Routes.js:

val().my_function_in_api // and so forth

Here is the complete code snippet:

var val = require('./api.js') //ensure to include the "./" before api.js

modules.exports = function(app){
app.get('/test_function',function(req,res){
val().my_function_in_api(req,res)}) // don't forget the parentheses following val

Answer №2

In my opinion, it is essential to include api.js using the following line of code: var value = require("./api.js"). Make sure to remember to add ./ when requiring files that you have created.

Routes.js

var value = require('./api.js') //note the use of "./" before api.js

modules.exports = function(app){
  app.get('/test_function',function(req,res){
    value.my_function_in_api(req,res)})

api.js

module.exports = (function() {
  return {
    my_function_in_api: function(req,res) {
      // carry out the task}})

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

Extract image file name and date information (day, month, year) from an HTML form using Angular

The content of the register.component.ts file can be found below: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-register', templateUrl: './register.component.html', styleUrls: [&apo ...

What is the best way to input data into nedb across various lines?

There is a requirement to store each element of an array into separate lines while saving it in the NEDB database. The idea is to add "\r\n" after every element, like this: Currently, I am doing the following: usernames = ["name1","name2","name3 ...

Unable to execute application due to invalid element type

I'm just diving into learning React Native, and as I attempt to launch my app, an error message pops up: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Verif ...

After upgrading from Vuetify version 1.5 to 2.0.18, an issue arises with modules not being found

I encountered the following error: module not found error To address this issue, I took the following steps: Commented out import 'vuetify/src/stylus/main.styl' in the src/plugins/vuetify.js file. Added import 'vuetify/src/styles/main. ...

The ideal scenarios for employing functional setState

Lately, I've been delving into the world of React, immersing myself in tutorials and explanations on how to manipulate elements in different ways. One aspect that has caught my interest is the setState function, which allows you to update or override ...

Issues arise with loading AngularJS directive properly within the context of service utilization

Currently, I am diving into the world of JavaScript and tackling a project that involves fetching data from MongoDB. My task is to write code in AngularJS to create a pie chart using Highcharts. Surprisingly, everything works smoothly when I solely use an ...

I am unable to utilize the Web Share API for sharing a file within my React app written in TypeScript

Trying to launch a WebApp for sharing files has been quite a challenge. After some thorough research, I stumbled upon the Web Share API which seemed like the perfect solution based on standard practices. The documentation provided a clear outline of how it ...

"Encountering a net::ERR_UNKNOWN_URL_SCHEME error message when making an Ajax post request

I'm encountering an issue while attempting to make a post call using Ajax from my frontend to my Express server. The error message I'm getting is net::ERR_UNKNOWN_URL_SCHEME. Here's the code snippet for the Ajax request: function sendStep ...

Is there a way to retrieve an image from the Youtube search feature within a React application?

async function searchYouTube(query ) { query = encodeURIComponent(query); const response = await fetch("https://youtube-search-results.p.rapidapi.com/youtube-search/?q=" + query, { "method": "GET", maxResult: 2, "headers": { ...

Verification of user input upon clicking the submit button within a form

I am encountering an issue with validating my form, as no errors are displayed in the console. I have followed the instructions provided by Bootstrap documentation but to no avail. My aim is to implement a feature where clicking on the button triggers an a ...

Creating dynamic form fields in Flask WTForm based on user's previous selection is a useful feature that can be achieved with some

I am interested in developing a form that dynamically generates different text area fields based on the selection made in a dropdown menu beforehand. Specifically, the idea is to create projects of various categories where, for instance, if a user chooses ...

Setting up a React Package by reinstalling a git repository

While immersing myself in learning React JS, I decided to create a git repository containing React components that could be exported and used or installed as a package in a separate React application. I developed some UI Components in a git repository and ...

Upon loading the webpage, the Angular Material form automatically scrolls downwards

Recently, I put together a lengthy form with the help of Angular Material. The issue I'm facing is that when the page loads, it doesn't start from the beginning of the form but rather somewhere in the middle. Could this be a browser-related probl ...

Top eCommerce frameworks optimized for Web2 and SaaS integration

Are there any payment and eCommerce frameworks that can seamlessly integrate with a REST-based application right out of the box? My server is Java-based, but I've found limited options in this area. I'm open to wrapping my interface with another ...

Change the value of a separate field when there is a change in the value of one field

I am currently utilizing Angular Schema Form and my goal is to assign a model property to null when the value of a field changes. I have attempted to utilize the onChange method in the form definition as shown below: { key: '7_11', type: &ap ...

Directive fails to trigger following modification of textarea model

There is a block of text containing newline separators and URLs: In the first row\n you can Find me at http://www.example.com and also\n at http://stackoverflow.com. The goal is to update the values in ng-repeat after clicking the copy button. ...

Ensure that the input remains below ten

My goal here is to ensure that the value in an input element is a non-zero digit (0<x<=9). Here's the HTML tag I'm using: <input type="number" class="cell"> I've experimented with various JavaScript solutions, but so far none h ...

Issue with Jquery Slider Showing Empty Slide

Currently addressing issues with the slider on my site, which utilizes SlidesJS at . There seems to be a problem where it shows a blank slide inexplicably. After investigating, I noticed that a list element is being added to the pagination class, but I can ...

The entire DOM refreshes when a user updates the input field

In my React component, I am managing two states: inputText and students. The inputText state tracks the value of an input field, while the students state is an array used to populate a list of student names. The issue arises when the inputText state change ...

Calculation Error in JavaScript/JQuery

I've been working on a JavaScript function to calculate the sum of values entered into textboxes, but it seems to be giving me inaccurate results in certain cases. Check out the FIDDLE here Enter values : 234.32 and 32.34 Result: 266.6599999999999 ...