Ayush
Ayush

Reputation: 323

why are my static files failing to load response

i am trying to get data from a html form using express but when i try to load my static files with app.use(express.static(__dirname+"public"); in the devtools it is showing that it failed to load my response data can anyone tell me why this is happening and a solution for this enter image description here

enter image description here

here is the code in the first image

 const express = require('express');

 const bodyParser = require('body-parser');

 const app = express();




app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname+'public'));


app.get("/",function(req,res){
  res.sendFile(__dirname + "/public/bmiCalculator.html");
});

app.post("/",function(req,res){
  console.log(req.body);
  res.send("hello");
});


app.listen(300,function(){
  console.log("Server Hosted OK");
});

Upvotes: 0

Views: 312

Answers (1)

ischenkodv
ischenkodv

Reputation: 4255

The path provided to the static middleware is incorrect. The following code __dirname+'public' will give you Calculatorpublic, i.e. / is missing. What you can do is to add the / before public:

app.use(express.static(__dirname, "/public"));

Another variation:

const path = require('path');
app.use(express.static(path.join(__dirname, "public")));

Upvotes: 1

Related Questions