Wednesday, 19 April 2017

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!



via Vinicius Martinson

No comments:

Post a Comment