Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

76
Vistas
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!

8 months ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

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;
8 months ago · Santiago Trujillo Denunciar

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;
8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.