Pratap simha
Pratap simha

Reputation: 11

How to import CSS and JS files in Node.js without using view engines?

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'));
})

Upvotes: 1

Views: 51

Answers (1)

Dennis Hadzialic
Dennis Hadzialic

Reputation: 11

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!

Upvotes: 1

Related Questions