2

This is my project folder structure:

XYZ_PROJECT_FOLDER

  ASSETS
    CSS
    IMAGES
    JS
    VENDOR

  CONTACT.html
  INDEX.html
  INDEX.js

And this is the code inside the INDEX.js file to render all the static files and other routing code:


    const express = require("express");
    const nodemailer = require("nodemailer");
    const multiparty = require("multiparty");
    require("dotenv").config();
    
    
    const app = express();
    
    app.use(express.static('assets'));
    
    app.route("/").get(function (req,res){
        res.sendFile(process.cwd()+"/index.html")
    })
    
    app.route("/contact").get(function (req,res){
        res.sendFile(process.cwd()+"/contact-form.html")
    })
    
    const PORT = process.env.PORT || 5000;
    app.listen(PORT, () => {
      console.log(`Listening on port ${PORT}...`);
    });

Whenever I run the command "node INDEX.js" it works all fine but it is not rendering any css, image, or js scripts any leads would be appreciated.

2 Answers 2

1

You can have Express serve static files to resolve this. Here's an example:

app.use(express.static(path.join(__dirname, 'client', 'build')));
Sign up to request clarification or add additional context in comments.

Comments

-1

Hey so the issue was in the HTML code I was using the whole path i.e. assets/css/style.css instead of using /css/style.css

changed the path in HTML and it worked.

Comments

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.