Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
How to get variable value on express that's on another file?

How to get variable value, if the variable was inside app.use

chatbot.js:

const express = require('express');
const app = express.Router();


app.use(['/chat', '/chatbot'], (req, res, next) => {
const bot = req.query.bot;
const client = req.query.client;
const text = req.query.text;
const json = require('./json/chatbot')
        
const result = JSON.parse(JSON.stringify(json.result[req.query.text].reply));

  console.log(result)
  res.json({'reply': result});
});

module.exports = app;

json/chatbot.js:

const botFile = require('../chatbot');
const bot = botFile.app.bot;


const data1 = ["Hi!", "Yo!", "Wassup", `Hello ${client}`]
const rep1 = data1[Math.round(Math.random() * data1.length)]

const json = {
        "result": {
"hi": {
    "reply": rep1,
    "id": 0
},
"hello": {
    "reply": rep1,
    "id": 0
},
"sup": {
    "reply": rep1,
    "id": 0
},
"yo": {
    "reply": rep1 ,
    "id": 0
},
"who are you": {
    "reply": `I'am ${bot}`,
    "id": 1
}
}}

module.exports = json;

USED URL:

https://api.domain.repl.co/api/v1/chat?client=ClientTEST&bot=Chat+Bot&text=who+are+you

It should replied

{"reply": "I'am Chat Bot"}

But instead, it replied

{"reply": "I'am undefined"}

There's no error in console

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Maybe something like this:

chatbot.js

const express = require('express');
const app = express.Router();
const getBot = require('./getBot.js')


app.use(['/chat', '/chatbot'], (req, res, next) => {
const bot = req.query.bot;
const client = req.query.client;
const text = req.query.text;

const json= getBot(client, bot);

const result = JSON.parse(JSON.stringify(json.result[req.query.text].reply));

  console.log(result)
  res.json({'reply': result});
});

module.exports = app;

getBot.js

function getBot(client, bot){
  const data1 = ["Hi!", "Yo!", "Wassup", `Hello ${client}`]
  const rep1 = data1[Math.round(Math.random() * data1.length)]
  
  return {
          "result": {
  "hi": {
      "reply": rep1,
      "id": 0
  },
  "hello": {
      "reply": rep1,
      "id": 0
  },
  "sup": {
      "reply": rep1,
      "id": 0
  },
  "yo": {
      "reply": rep1 ,
      "id": 0
  },
  "who are you": {
      "reply": `I'am ${bot}`,
      "id": 1
  }
  }}

}

module.exports = getBot;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda