Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs程序我无法获得输出,我在哪里写的逻辑错误?_Node.js - Fatal编程技术网

Node.js nodejs程序我无法获得输出,我在哪里写的逻辑错误?

Node.js nodejs程序我无法获得输出,我在哪里写的逻辑错误?,node.js,Node.js,http:此url持续加载,但浏览器上没有显示网页您误用了url模块。要访问当前路由,可以使用request.url,并且请求对象上没有setHeader方法 下面是一个工作示例 consthttp=requirehttp; 常数fs=所需参数; const path=requirepath; const hostname=127.0.0.1; 常数端口=3001; 让服务器=http.createServerrequest,响应=>{ 如果request.url==/index | | req

http:此url持续加载,但浏览器上没有显示网页

您误用了url模块。要访问当前路由,可以使用request.url,并且请求对象上没有setHeader方法

下面是一个工作示例

consthttp=requirehttp; 常数fs=所需参数; const path=requirepath; const hostname=127.0.0.1; 常数端口=3001; 让服务器=http.createServerrequest,响应=>{ 如果request.url==/index | | request.url==/{ fs.readFilepath.join\uuuu dirname,index.html,utf8,err,data=>{ 如果犯了错误,就扔出错误; response.writeHead200,{内容类型:text/html}; response.enddata; }; }否则,如果request.url===/请联系{ fs.readFilepath.join\uuuu dirname,contact.html,utf8,err,data=>{ 如果犯了错误,就扔出错误; response.writeHead200,{内容类型:text/html}; response.enddata; response.enddata; }; }else if request.url===/about{ fs.readFilepath.join\uuuu dirname,about.html,utf8,err,data=>{ 如果犯了错误,就扔出错误; response.writeHead200,{内容类型:text/html}; response.enddata; response.enddata; }; }否则如果request.url===/profile{ fs.readFilepath.join\uuuu dirname、profile.html、utf8、err、data=>{ 如果犯了错误,就扔出错误; response.writeHead200,{内容类型:text/html}; response.enddata; response.enddata; }; }否则如果request.url===/service{ fs.readFilepath.join\uuuu dirname、service.html、utf8、err、data=>{ 如果犯了错误,就扔出错误; response.writeHead200,{内容类型:text/html}; response.enddata; response.enddata; }; } }; server.listenport,主机名=>{ log`服务器启动于http://${hostname}:${port}`; }; 您可以共享package.json文件吗。另外,您当前的输出是什么,预期的输出应该是什么?
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3001;
let server = http.createServer((request,response) => {
        response.statusCode=200;
        response.setHeader('content-Type','text/html');
        if(url==='/index') {
            fs.readFile(path.join(__dirname,'index.html'), 'utf8', (err,data) => {
                if(err) throw err; response.end(data)
            });
        } else if(url==='/contact') {
            fs.readFile(path.join(__dirname,'contact.html'), 'utf8', (err,data)=> {
                if(err)throw err;response.end(data)
            });
        }

    else if (url==='/about') {
        fs.readFile(path.join(__dirname,'about.html'),'utf8',(err,data) => {
            if (err) throw err;
            response.end(data);
        })
    }
    else if (url==='/profile') {
        fs.readFile(path.join(__dirname,'profile.html'),'utf8', (err,data) => {
            if (err) throw err;
            response.end(data)
        })
    }

    else if (url==='/service') {
        fs.readFile(path.join(__dirname,'service.html'), 'utf8', (err,data) => {
            if(err)throw err;
            response.end(data)
        })
    }
});

server.listen(port,hostname, ()=> {
    console.log(`serevr is started at http://${hostname}:${port}`)
});