1

I recently started learning node.js and now I'm stuck. am now building a basic authentication webpage including Login and Register. The res.sendfile method is not importing CSS files. I did not opt for view engines as I find it a bit difficult and messy. Is there any way to import CSS files here? Here is my code. Excuse me if I've kept it too long, as it is my first time asking a question here.

This is my login GET route where I've used res.sendFile.

login.get("/",(req,res)=>{
  res.sendFile(path.join(__dirname, '/auth', 'login.html'));
})

1
  • add your css and js files directly to html file. Commented Oct 21, 2020 at 4:55

1 Answer 1

1

The problem I think is that it is not serving the files in the desired directory. Try adding this in the line above so it looks like this:

app.use(express.static("client/build"));

app.get("/",(req,res)=>{
  res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
})

I guess login and in the example I've provided above I chaged it to something more standard as app is the variable you've called express after init it. Send the whole nodeJS server-file otherwise.

Don't hesitate to ask if you have any questions!

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

6 Comments

Thank you very much. As a matter of fact, I do have a few other questions. Can I get your email or some other mode of contact, it it isn't a problem for you?
@Pratapsimha Consider posting your questions on Stack Overflow, so all can benefit from the questions and answers.
@DennisHadzialic If you're using express.static() in this way, there should be no need for the specific handler for index.html, as that's already included by Express.
@DennisHadzialic, it didn't work as well. CSS is not at all imported. I tried express.static(path.join(__dirname, "client")), and it worked. But, the same won't work when it's inside a route,i.e, app.get("/")
@Pratapsimha Do you have a Github-repository to share so I can take a deeper look?
|

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.