0

I made a function to check if someone is logged in on the site in the user controller module:

exports.isLoggedIn = function(req, res, next) {
    if (req.user) {
        return true;
    } else {
        return false;
    }
};

I have no idea how I want to use this in a imported script on the client side. I couldn't find a good solution on the web so I thought I would ask the question myself.

If I import the script in the .html I get an error that says it doesnt know the require() function that node has.

I hope someone can help me :)

5
  • 2
    You're looking for Browserify. But beware that you can't run server middleware in the browser because there is no request.. Commented May 30, 2016 at 20:51
  • So I can't use the function to check if the player is logged in that way? Commented May 30, 2016 at 20:52
  • You need to somehow communicate that state to the client. Commented May 30, 2016 at 20:52
  • Alright thanks, going to have to find out how to do that haha Commented May 30, 2016 at 20:52
  • No, the only thing you can do without bundler like browserify/Webpack is to do a new request to your server (with the user id for example) to give you back if he's connected Commented May 30, 2016 at 20:54

1 Answer 1

2

If you want client access to some data that is only available on the server, then you need to send an ajax call from the client to your server and you need to create a route on your server to respond to that ajax call.

Client code runs only on the client and has no direct access to any data on the server.

Server code runs only on the server and has no direct access to any data on the client.

To communicate between the two, you have to send a request from one to the other and then return a response. Usually this is done with an Ajax call sent from client to server. You could also establish a webSocket connection between the two and then either client or server could send data to the other.

The server also has the opportunity, when creating the original page content, to embed settings or values in the page itself, either as Javascript variables, as HTML values or even as a cookie. This obviously has to be done ahead of time when the page is rendered so it can't be a request that the client comes up with later after the page has been rendered to the client.


FYI, in the particular example you show, it is common for a client to be able to tell if it is logged in via some state in the page (either the presence of a particular cookie) or something else embedded in the page by the server. This isn't necessarily secure and isn't the way the server would tell if a request was logged in, but it usually suffices for client-side logic to decide how it wants to behave.

Sign up to request clarification or add additional context in comments.

3 Comments

Or you could just include the state in the initial pageload.
@SLaks - Thx, I added that to my answer.
It worked, I got data back :D. Thank you @SLaks and jfriend :D, I used an ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.