Node.js node js my localhost服务器在';http://localhost:3000/ ';但是对于链接{http://localhost:3000/ index.html}工作正常

Node.js node js my localhost服务器在';http://localhost:3000/ ';但是对于链接{http://localhost:3000/ index.html}工作正常,node.js,localhost,Node.js,Localhost,node js my localhost服务器在“”上工作不正常http://localhost:3000/ “但对于林克来说{ http://localhost:3000/ index.html}工作正常 const http = require('http'); const fs =require('fs'); const path=require('path'); const hostnam

node js my localhost服务器在“”上工作不正常http://localhost:3000/ “但对于林克来说{ http://localhost:3000/ index.html}工作正常

        const http = require('http');
        const fs =require('fs');
        const path=require('path');
        
        
        const hostname = 'localhost';
        const port =3000;
        
        const server = http.createServer((req,res) =>{
        
        
            console.log("Request for ' " + req.url +" ' by mehtod "+ req.method);
            
            if(req.method ==='GET'){
                var fileURL;
                if(req.url === '/') {fileURL ='index.html';}
                else{ fileURL = req.url;}
        
                var filePath = path.resolve('./public'+fileURL);
                const fileExt = path.extname(filePath);
                if (fileExt == '.html') {
                  fs.exists(filePath, (exists) => {
                    if (!exists) {
                      res.statusCode = 404;
                      res.setHeader('Content-Type', 'text/html');
                      res.end('<html><body><h1>Error 404: ' + fileURL + 
                                  ' not found</h1></body></html>');
                      return;
                    }
        
                        res.statusCode=200;
                        res.setHeader('content-Type','text/html');
                        fs.createReadStream(filePath).pipe(res);
        
        
                    })
                }
                else{
                    res.statusCode=404;
                    res.setHeader('content-Type','text/html');
                    res.end('<html><body><h1>Error 404 : '+fileURL + 'not an html file  </h1></body></html>');
                    return;
        
                }
        
        
            } 
              else{
                res.statusCode=404;
                res.setHeader('content-Type','text/html');
                res.end('<html><body><h1>Error 404 : '+req.method + 'not supportive  </h1></body></html>');
                return;
        
        
            }
           
        })
        
        server.listen(port,hostname,()=>{
            console.log(`Server running at http://${hostname}:${port}`);
        })
consthttp=require('http');
常数fs=要求('fs');
const path=require('path');
常量主机名='localhost';
常数端口=3000;
const server=http.createServer((req,res)=>{
console.log(“通过方法“+req.method”请求“”+req.url+”);
if(req.method==='GET'){
var文件URL;
如果(req.url==='/'){fileURL='index.html';}
else{fileURL=req.url;}
var filePath=path.resolve('./public'+fileURL);
const fileExt=path.extname(filePath);
如果(fileExt=='.html'){
fs.exists(文件路径,(exists)=>{
如果(!存在){
res.statusCode=404;
res.setHeader('Content-Type','text/html');
res.end('Error 404:'+fileURL+
“未找到”);
返回;
}
res.statusCode=200;
res.setHeader('content-Type','text/html');
createReadStream(filePath).pipe(res);
})
}
否则{
res.statusCode=404;
res.setHeader('content-Type','text/html');
res.end('错误404:'+fileURL+'不是html文件');
返回;
}
} 
否则{
res.statusCode=404;
res.setHeader('content-Type','text/html');
res.end('Error 404:'+req.method+'not supporting');
返回;
}
})
侦听(端口、主机名,()=>{
log(`Server running at http://${hostname}:${port}`);
})
node js my localhost服务器在“”上工作不正常http://localhost:3000/ “但对于林克来说{ http://localhost:3000/ index.html}工作正常

        const http = require('http');
        const fs =require('fs');
        const path=require('path');
        
        
        const hostname = 'localhost';
        const port =3000;
        
        const server = http.createServer((req,res) =>{
        
        
            console.log("Request for ' " + req.url +" ' by mehtod "+ req.method);
            
            if(req.method ==='GET'){
                var fileURL;
                if(req.url === '/') {fileURL ='index.html';}
                else{ fileURL = req.url;}
        
                var filePath = path.resolve('./public'+fileURL);
                const fileExt = path.extname(filePath);
                if (fileExt == '.html') {
                  fs.exists(filePath, (exists) => {
                    if (!exists) {
                      res.statusCode = 404;
                      res.setHeader('Content-Type', 'text/html');
                      res.end('<html><body><h1>Error 404: ' + fileURL + 
                                  ' not found</h1></body></html>');
                      return;
                    }
        
                        res.statusCode=200;
                        res.setHeader('content-Type','text/html');
                        fs.createReadStream(filePath).pipe(res);
        
        
                    })
                }
                else{
                    res.statusCode=404;
                    res.setHeader('content-Type','text/html');
                    res.end('<html><body><h1>Error 404 : '+fileURL + 'not an html file  </h1></body></html>');
                    return;
        
                }
        
        
            } 
              else{
                res.statusCode=404;
                res.setHeader('content-Type','text/html');
                res.end('<html><body><h1>Error 404 : '+req.method + 'not supportive  </h1></body></html>');
                return;
        
        
            }
           
        })
        
        server.listen(port,hostname,()=>{
            console.log(`Server running at http://${hostname}:${port}`);
        })