Nodejs error res is not defined?

running nodejs Times error res is not defined, please take a look at why?

var http = require("http")
var path = require("path")
var fs = require("fs")
var url = require("url")
var server = http.createServer(function(req, res){
   var pathObj = url.parse(req.url, true)
  console.log(pathObj)
  console.log("~")
  console.log(req)

})
fs.readFile("./hello.html",function(err,fileContent){
    if(err){
        console.log(404)
        res.writeHead(404,"not found")
        res.end()
        }else{
             res.writeHead(200, "OK")
             console.log("OK")
            res.write(fileContent)
            res.end()

        }

    
})

server.listen(8080)
Feb.28,2021

you may need to learn javascript first and then learn nodejs, fs.readFile there is no definition of res under the scope of the callback function.

Menu