【问题标题】:Node.js HTML response bodyNode.js HTML 响应正文
【发布时间】:2018-03-03 04:39:28
【问题描述】:

我正在使用下面的代码将一些 JSON 数据发布到一个 url,作为响应,我得到一个 HTML 页面。

var request = require('request');
request.post({
     url: "URL OF A WEBSITE",
     headers: {
        "Content-Type": "application/json"
     },
     body: {
       my_json_obj
     },
     json:true
}, function(error, response, body){
   console.log(error);
   console.log(JSON.stringify(response));
   console.log(body);
});

这段代码运行良好我在body 标记中获得了一个HTML 页面。 我想在浏览器中加载该 HTML 页面。我应该怎么做,我知道这是一个非常基本的问题,但我对 node.js 很陌生,请帮助我?

【问题讨论】:

  • 您需要使用 Express 或 node.js 的内置服务器库来提供该内容。现在,您所做的就是获取它。
  • @zero298 那么这是否意味着我必须在 node.js 或类似的东西中创建一个服务器?如果您可以举个例子或分享任何有例子的链接,那将会很有帮助。谢谢。
  • 节点文档给出了一个如何创建http服务器的例子:nodejs.org/docs/latest-v4.x/api/…
  • http 服务器的节点实现是非常手动的方法。很高兴知道它是如何工作的,但最常见的页面服务包来自 expressJs npmjs.com/package/express

标签: html angularjs node.js http-post


【解决方案1】:

您使用的是 Express js 吗?在使用 Node js 应用程序时,它会让事情变得更容易。

查看 Express 路由: https://medium.com/javascript-scene/introduction-to-node-express-90c431f9e6fd

您可以使用以下命令在终端中创建 Express 样板:

express yourappname

然后您可以将您的 html/css/js 文件放入您刚刚生成的 express-app -> 'public' 文件夹中。

之后,您通过执行以下操作在 app.js 中创建路由:

// exampledomain.com/
// Loading the file index.html when user navigates to '/'
app.get('/', function(req, res){

    res.sendFile(path.join(__dirname + '/public/index.html'));

});

// or

// exampledomain.com/getFileURL
// POST request (from your ajax) to '/getFileURL'
// which will then load the about.html file into the browser
app.post('/getFileURL', function(req, res){

    // Do more JSON/data handling & Node js stuff here then
    // send something back

    res.sendFile(path.join(__dirname + '/public/about.html'));

});

Express js 有更多有用的功能,但我认为路由是您现在需要的。

附言使用 Node js 时非常有用的工具:

  • Postman(测试 ajax/express/Nodejs 路由和响应)
  • Nodemon(保存时自动启动 Nodejs 服务器)

【讨论】:

    【解决方案2】:

    按照Express "Hello World" example 并在路由处理程序中使用您的请求调用:

    /*jslint node:true, esversion:6*/
    
    "use strict";
    
    const request = require("request"),
        express = require("express"),
        app = express();
    
    let my_json_obj = {},
        URL_OF_WEBSITE = "http://www.google.com";
    
    app.get("/", function (req, res) {
        request.post({
            url: URL_OF_WEBSITE,
            headers: {
                "Content-Type": "application/json"
            },
            body: {
                my_json_obj
            },
            json: true
        }, function (error, response, body) {
            if (error) {
                console.log(error);
                return res.sendStatus(404);
            }
            console.log(JSON.stringify(response));
            console.log(body);
            res.send(body);
        });
    });
    
    app.listen(3000, function () {
        console.log("Example app listening on port 3000!");
    });
    

    node.js 确实有自己的方式来制作服务器,但为了简洁和方便,我只推荐使用 Express。

    【讨论】:

    • res.send(body);此行在浏览器中加载 html,但未为该网页加载 css。知道为什么会这样吗?
    • @Syn3sthete 打开浏览器调试工具。你有任何错误吗? CSS是否来自同一个域?可能不是。如果您尝试代理网站,只需使用代理模块。
    • 是的,你是对的,url 是我的本地主机,但 CSS 引用错误显示了第三方网站的 url。如何将代理模块添加到上述代码中?可以编辑吗?
    • @Syn3sthete 我建议您只需遵循任何可用模块的文档,例如 nodejitsu/node-http-proxy
    猜你喜欢
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2021-03-19
    相关资源
    最近更新 更多