Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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如何呈现视图?_Node.js - Fatal编程技术网

Node.js如何呈现视图?

Node.js如何呈现视图?,node.js,Node.js,在带有单个app.js文件和一个index.html文档的basic Node.js应用程序中,在app.js中指定了以下内容,然后启动服务器并访问localhost:8080就可以了: server = http.createServer( function(req, res) { fs.readFile('index.html', function(err, page) { res.writeHead(200, {'Content-Type': 'text/html'

在带有单个
app.js
文件和一个
index.html
文档的basic Node.js应用程序中,在
app.js
中指定了以下内容,然后启动服务器并访问
localhost:8080
就可以了:

server = http.createServer( function(req, res) {
    fs.readFile('index.html', function(err, page) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(page);
        res.end();
    });

    fs.readFile('new.html', function(err, page) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(page);
        res.end();
    });
});

server.listen(8080);
但是,当将
index.html
复制到另一个文件(如
new.html
)并编辑某些内容,然后将链接添加到
index.html
链接到新页面时,单击该链接将呈现与
index.html
中相同的内容。事实上,链接到任何不存在的html页面都会将后续页面附加到URL,但会继续显示
index.html
的内容

建议将fs.readFile行改写为:

fs.readFile(req.url, function(err, page) { ...

然后转到
localhost:8080
加载
new.html
的内容,原因我不明白。如何呈现这些视图?

因为您需要在请求访问url(req.url)上设置条件

现在,不管您的url如何,它都会关闭第一个
res.end()
上的响应,并且永远不会到达代码的其余部分(确实如此,但之前已经触发了响应,因此没有任何效果)

试试这个:

server = http.createServer( function(req, res) {

    if (req.url == '/index.html') { //will be executed only on index.html
        fs.readFile('index.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }

    if (req.url == '/new.html') { //will be executed only for new.html
        fs.readFile('new.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }
});

server.listen(8080);

根据给出的其他答案,并推荐express。但首先,对“node.js如何呈现视图”这个问题的简短回答是:它没有

当您构建一个节点应用程序时,您正在构建一个小型web服务器,使用最小构建块节点可以提供类似http.createServer()的功能。您可以编写逻辑来选择响应请求发送的内容

也可以使用现有的框架,如Express。以下是使用express的解决方案:

安装express:

npm install express
然后,在app.js中:

var express = require('express');

var app = express.createServer();

app.get('/index.html', function(req,res) {
    res.render('index.html');
});

app.get('/new.html', function(req,res) {
    res.render('new.html');
});

app.listen(8080)
您还可以通过在createServer行之前添加以下内容,将EJS或Jade用作模板语言:

app.set('view engine', 'ejs');
或:


你真的应该看看Express(node web framework):-它比http.createServer更容易使用。我将第一个条件设置为if(req.url=='/'),第二个条件设置为
,否则如果(req.url=='/new.html')
则它可以工作,但我不喜欢这个解决方案。这是node的工作方式,您必须编写发送请求的代码。或者,您也可以寻找一个模块作为一个web框架,以便更方便地使用。看见
app.set('view engine', 'jade');