server is directing to my c:/ directory instead of message

Viewed 25

What I am trying to do is when the user clicks the calculate button(not doing any calculations yet) it should redirect them to a new screen with with a response message which says "Thanks for posting that!". But instead of the message it would display my entire C:\ directory, I feel like it has something to do with the "/" as the route but in the tutorial that what they used.

here's my index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>calculator</title>
  </head>
  <body>
    <h1>Calculator</h1>
    <form class="" action="/" method="post"> <!-- Send to home route-->
      <input type="text" name="num1" placeholder="First Number">
      <input type="text" name="num2" placeholder="Second Number">
      <button type="submit" name="submit">Calculate</button>
    </form>

  </body>
</html>

Here's the server calculator.js

const express = require("express");
const app = express();

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


app.post("/", function(req,res){
  res.send("Thanks for posting that!");
});


app.listen(3000,function(req,res){
  console.log("server up");
});
1 Answers

The relative URL is computed from where the browser opened the HTML document from.

You opened it from a file on your C: drive (probably by double clicking on it in Windows Explorer) so it is resolved to the root of your C: drive.

If you want it to resolve to the root of the website hosted by your Express server then you need to open the original HTML document from the server in the first place.

Type http://localhost:3000 into the address bar of your browser.

Related