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

2 Answers2

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")
})
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
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.

goto
  • 4,336
  • 15
  • 20