javascript - http请求不会执行

标签 javascript node.js

我是 JS 和 Node 方面的真正菜鸟,我正在尝试从 REST API 接收到的 JSON 渲染 JADE View 。当我作为独立运行 http.request 时,它工作得很好,但是当我开始添加模块和渲染雄蕊时,我无法执行 http 请求函数。

当我在调试中运行它时,它只是跳到结束语句。我不明白为什么。

如有任何帮助,TIA 将不胜感激。

var http = require('http');

module.exports = function() {

    var options = {
        host: '41.193.214.130',
        port: 2510,
        path: '/eiftidemo/clt_list',
        method: 'GET'
    };

    var clientsDatag;

    http.request(options, function(res) {
        var body = '';
        //none of these statemnst excecute

        res.on('data', function(chunk) {
            body += chunk;
        });

        res.on('end', function() {
            var clientsData = JSON.parse(body);

            var clientsDatag = clientsData;
            // this stament doesn't execute either
            debugger;

        });
    }).end();
    debugger;
    res.render('listlxr', {
        details: clientsDatag
    });
};

这是调用脚本:

var express = require('express');
var bodyParser = require('body-parser');
var tweetList = require('./tweet-list');
var clientList = require('./lxr-clients')
var app = express();

app.set('view engine', 'jade');
app.use(bodyParser.urlencoded({
    extended: false
}))


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

app.post('/get_tweets', function(req, res) {
    var screen_name = req.body.handle;

    var tweets = tweetList(res, screen_name);
});

app.get('/get_clients', function(req, res) {

    var clientd = clientList(res, req);
});

var server = app.listen(3000, function() {
    console.log('Our App is running at http://localhost:3000');
});

非常感谢任何可以提供帮助的人

最佳答案

app.get('/get_clients', function(req, res) {

    var options = {
        host: '41.193.214.130',
        port: 2510,
        path: '/eiftidemo/clt_list',
        method: 'GET'
    };

    http.request(options, function(details) {
        res.render('listlxr', {
            details: details
        });
    });

});

关于javascript - http请求不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40790054/

相关文章:

node.js - 如何制作基于角色的中间件以在nodejs和mongoose中以适当的权限访问api?

node.js - express.js + 手写笔 : Automatic stylus compiling doesn't work

node.js - then-redis中如何进行rpush操作?

javascript - 在 jQuery 中反转 $.each 输出

javascript - 具有多个输入的自定义 Knockout 绑定(bind),其中一个包含索引数组

javascript - 删除 Iframe 中的类

javascript - 从 JSON 数组中抓取并打印特定对象 - Javascript

javascript - IE8 jquery json响应触发下载

JavaScript - 如何列出对象的属性?

node.js - 检索属于 HTTP 请求的 socket.io 连接