Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
Node.js + Express: file structure and communication

I have a login application but I am having trouble communicating the credentials of the form (email and password) to my existing .js files, where it performs some logic and retrieves some info.

On my login page I have a form with a POST method. Then, I have a main.js that performs the login:

main.js

module.exports = {
  returnSessionToken: function(success, error) {
    var email = email_X;
    var pwd =  password_Y;

   [...]
function(error, response, body) {
   var login = JSON.parse(body);
   success(login.sessionToken)
}

And then I have a index.js where I retrieve some info of the logged user:

index.js

var authToken = require("./main");

authToken.returnSessionToken((result) => {
   'my_website_token': result,
   [...]
}

In my express project, I have these two files in a js folder. What I am trying to do is getting the email and password of the form of the login page and pass it to my main.js file (email_X and password_Y) and then call index.js. I know how to get the req.body.email and req.body.password in the routes folder, but stil can't figure out how to make these files communicate.

I have also tried to edit the app.js file into:

app.js

var login = require('./js/main');

and then,

app.use('/myaccount', login);

But no success. Any help will be very much appreciated. Thanks!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I'm slightly confused by the details and what looks like some missing steps, but it looks like the problem may be caused by the way you're setting up and referencing your main.js module.

app.use('/myaccount', login) will send your main.js module two objects, generally referenced as req and res. req contains information about the http request. I believe the data from a form HTTP post is sent in req.body.

See example below:

index.js

var express = require('express');
var login = require('./login');
var app = express(); 

app.use('/login', login); // mount the sub app

login.js

var express = require('express'); 
var login = express(); 

login.post('/', function (req, res) {

  console.log(req.body); // should print your form data

  // do your login logic here

  res.status(200).end(); // Sends http response back
});

module.exports = login;
about 4 years ago · Santiago Trujillo Relatório

0

For future reference, here's a working example. Thank you Ryan Villanueva for the help!

main.js

module.exports = {
  returnSessionToken: function(email, pwd, success, fail) {

    var email = email;
    var pwd =  pwd;
    success(token)
    [...]
}

index.js

module.exports = {
  returnUserData: function(authToken, success) {
  [...]
  'my_website_token': authToken,
}

app.js

var myaccount = require('./routes/myaccount');
app.use('/myaccount', myaccount);

routes/myaccount.js

var express = require('express');
var router = express.Router();
var login = require('../js/main')

router.post('/', function(req, res) {

  var email = req.body.email
  var password = req.body.password
  login.returnSessionToken(email, password, (token) => {
      console.log("return token: ", token)
      var logged = require('../js/index')
      logged.returnUserData(token, (myData) => {
        res.render('myaccount', { myData: myData });
      })

    },
  (fail) => {console.log(fail)})

})

module.exports = router;
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda