Javascript 使用nodeJS将文章从X加载到现有的X.html

Javascript 使用nodeJS将文章从X加载到现有的X.html,javascript,html,node.js,Javascript,Html,Node.js,是啊,这又是一个难题。我在谷歌上搜索过,试图找到答案,但由于nodeJS还不如php那么流行,所以它并不那么容易 假设我有一个index.html文件,如下所示: <html> <body> <div id="container"> <div id="title">Articles</div> <div id="articles"> <!--Load information from n

是啊,这又是一个难题。我在谷歌上搜索过,试图找到答案,但由于nodeJS还不如php那么流行,所以它并不那么容易

假设我有一个index.html文件,如下所示:

<html>
<body>
<div id="container">
    <div id="title">Articles</div>
    <div id="articles">
        <!--Load information from nodeJS-->
    </div>
</div>
</body>
</html>
var http = require('http');

var server = http.createServer(function(request, response) {
    response.write("<div class='articletitle'>ABC</div>");
    response.write("<div class='articletext'>This is a test article</div>");
    response.end();
});

server.listen(3000);
html
  body
    div#container
      div#title Articles
        div#articles
          each article in articles
            div.articletitle= article.title
            div.articletext= article.text

文章
我有一个nodeJS文件,如下所示:

<html>
<body>
<div id="container">
    <div id="title">Articles</div>
    <div id="articles">
        <!--Load information from nodeJS-->
    </div>
</div>
</body>
</html>
var http = require('http');

var server = http.createServer(function(request, response) {
    response.write("<div class='articletitle'>ABC</div>");
    response.write("<div class='articletext'>This is a test article</div>");
    response.end();
});

server.listen(3000);
html
  body
    div#container
      div#title Articles
        div#articles
          each article in articles
            div.articletitle= article.title
            div.articletext= article.text
var http=require('http');
var server=http.createServer(函数(请求、响应){
回复。填写(“ABC”);
回答。写下(“这是一篇测试文章”);
response.end();
});
听(3000);
我想要什么

  • 我想从nodeJS输出中获取文章,并将其放入
    #articles

您必须使用haml或jade之类的模板引擎


此外,我建议您使用expressjs。

首先,由于您是NodeJS新手,我强烈建议您使用。 它封装了大多数节点所需的特性

要轻松实现你想要的,最好的方法就是使用一些

例如,您可以使用以下代码:

<html>
<body>
<div id="container">
    <div id="title">Articles</div>
    <div id="articles">
        <!--Load information from nodeJS-->
    </div>
</div>
</body>
</html>
var http = require('http');

var server = http.createServer(function(request, response) {
    response.write("<div class='articletitle'>ABC</div>");
    response.write("<div class='articletext'>This is a test article</div>");
    response.end();
});

server.listen(3000);
html
  body
    div#container
      div#title Articles
        div#articles
          each article in articles
            div.articletitle= article.title
            div.articletext= article.text
然后,您的NodeJS(使用ExpressJS框架)代码如下:

app.get('/', function (req, res) {
  var tmpl = {
    articles: [
      { title: 'ABC', text: 'This is a test article' },
      { title: 'XYZ', text: 'Just another random article' }
    ]
  };
  res.render('index', tmpl);
});

你需要一个模板引擎。一个简单的例子是EJS(它与您在PHP中所知道的非常接近),下面是一个示例(别忘了用npm安装它):

var http=require('http');
var ejs=需要('ejs');
//创建http服务器
http.createServer(函数(req,res){
res.writeHead(200,{'content-type':'text/html'});
//要渲染的数据
var articles=[{title:'Hello'}];
//呈现ejs文件
renderFile(uu dirname+'/views/articles.ejs',
{条款:条款},
函数(错误、结果){
//功成名就
如果(!err){
目的(结果);
}否则{
res.end(“发生错误”);
控制台日志(err);
}
}
);
}).听(3000);
//views/articles.ejs
文章

您的示例是错误的,因为它使用了Express语法。这个问题没有更新答案。谢谢你通知我!还有一个小问题*那么我需要将nodeJS链接到domain.com而不是index.html文件吗?我不明白你的意思。告诉我我对nodeJS的看法是否正确:“基本上nodeJS是一个编译器,它将代码编译成响应并发送给用户”它不是编译器,这是因为您需要在运行代码的计算机上安装NodeJS。在你们的案例中,你们在网络案例中使用NodeJS,但你们可以做得更多。好的,明白了!谢谢你的帮助