0

I a beginner with Express js and when I reload the server to show the HTML file display "Cannot get" this is photo from the console and its show som errors enter image description here

this my code server-side: enter image description here

and this is a photo from git bash and the server is workingand this is a photo from git bash and the server is working

and this is my HTML code enter image description here

help, please

9
  • Try app.get instead of app.route. And use the res variable of the callback. Commented Apr 5, 2020 at 22:21
  • 2
    Please don't post screenshots of your code. Copy and Paste it please. Commented Apr 5, 2020 at 22:22
  • 1
    For future reference, don't post screenshots of your code. Post the actual code as text. It will make it easier for others to copy and paste you code in order to help you. Commented Apr 5, 2020 at 22:22
  • ok thanks verymuch Commented Apr 5, 2020 at 22:29
  • @AbdelilahZaidane were you able to fix it? Commented Apr 5, 2020 at 22:29

2 Answers 2

1

Instead of app.route(), use app.get() like this

const express = require("express)
const path =  require("path")
const app= express()
app.get("/",(req,res)=>{
 res.sendFile(path.join(__dirname, './index.html'))
})
app.listen(3000,()=>{
 console.log("server running at port 3000")
})
Sign up to request clarification or add additional context in comments.

Comments

0

app.route takes a string as an argument and returns a single route - you're passing a callback function, so change your route handling to the following:

// use the appropriate HTTP verb
// since you're trying to serve the `index.html` file, 
// `get` should be used

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

Alternatively, you could just do the following:

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

Here's a working example:

// thecodingtrain/
//   index.js
//   home.html
//   package.json

const path = require("path")
const express = express()

const app = express()
const PORT = 3000

app.route("/")
  .get((req, res, next) => {
     res.sendFile(
       path.join(__dirname, "./home.html")
     )
  })

app.listen(PORT, () => {
  console.log(`Listening on port ${PORT})
})

Hope this helps.

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.