Why didn't I respond when I entered localhost:8080 after I started node?

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

        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()}
})}})
  //  var pathObj = url.parse(req.url, true)
  // console.log(pathObj)
  // console.log("~")
  // console.log(req)
    


server.listen(8080)
console.log("~")

clipboard.png

Feb.28,2021

your staticRoot call?


first of all, which part console.log does not respond, then the node service does not return anything, and staticRoot does not execute.


  1. the staticRoot method did not execute
  2. res.writeHead (404 http://nodejs.cn/api/http.html-sharphttp_response_writehead_statuscode_statusmessage_headers" recording not found') is written incorrectly by referring to http://nodejs.cn/api/http.htm..
Menu