0

I have my index.html, when I click on a button, it calls a angular service that use $resource.

var serviceRest = $resource(URL_API, null,
    {
       "connect" : { method: "GET", url: "http://localhost/login"}
     });

And then in my expressJS route I have this :

router.get('/',function(req,res){
    console.log("Hi there");
    res.sendFile('../public/views/login.html', {root: __dirname});
});

But it does nothing. Nothing happens but my console log display so my sendFile might be false... I stay on my index.html.

| -- Agenda
     | -- public
          | -- views
               | -- login.html
     | -- controllers
          | -- routes.js

My router file is in ./controllers, and my login.html is in ./public/views

EDIT :

Ok, so the problem is not a path problem. But, in all solution that I tried, only one gave me a good result.

res.sendFile(path.join(__dirname, '../public/views/login.html'));

After that, I asked myself "What I am returning in the Angular Service with the $resource. And this is at this moment that I saw that my connect() function :

var serviceRest = $resource(URL_API, null,
{ 
    "connect" : { method: "GET", url: "http://localhost/login"}
});

return {

    connect: function() {
        console.log("connect");
        console.log(serviceRest.connect());
        return serviceRest.connect();

    }
}

This console.log(serviceRest.connect()); shows me a Promise in the browser console. And this is a huge array with one character per element, that containing my login.html code lol. Funny but not help me haha

9
  • did you configure router to process /login? Commented Jun 6, 2017 at 8:00
  • What do you mean by "process /login" ? Yeah that's note router.get('/'.... But it's really /login, my console.log("Hi there"); is display when I click on button. So it enter in the router.get Commented Jun 6, 2017 at 8:04
  • If it's your ask, I have this yes on my server.js : app.use('/login', login); Commented Jun 6, 2017 at 8:06
  • your login.html is in /public/views but you are sending ../public/login.html??? Commented Jun 6, 2017 at 8:06
  • Oh yes, thanks. But it doesn't work after this edit... Commented Jun 6, 2017 at 8:09

1 Answer 1

1

root option is the directory from which you want to serve the files from. Assuming your folder structure is like below :

| yourApp
  | -- public/
       | -- views/
         |-- login.html
  | -- controllers/
       | -- routes.js

Try setting the root directory with yourApp

res.sendFile('public/views/login.html', {root: '../<root folder name>'});

Replace the "root folder name" above with the parent directory name of 'public' folder. Considering the above folder structure that would be :

res.sendFile('public/views/login.html', {root: '../yourApp'});
Sign up to request clarification or add additional context in comments.

2 Comments

In my case the "yourApp" folder is name "Agenda". So I try that you said to me : res.sendFile('public/views/login.html', {root: '../Agenda'}); And it's not working... I also tried with a really aboslute path juste like this res.sendFile('C:\\Users\\.....'); and the same result.. This res.sendFile makes me mad
I edited my question, I have more details about the problems

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.