nodeJS创建第一个应用

最近开始了解nodeJS。做了如下的一个功能:
使用nodeJS开启一个服务,然后调用XXX后台接口的数据,返回的数据格式如下,然后以表格的形式在页面中展示出来。
这里写图片描述

代码如下:

var http = require('http');
var request = require('request');

function handleData(arr){
    var content = '';
    for (var i = 0; i < arr.length; i++){
        content += '<tr><td>' + arr[i].id + '</td><td>' + arr[i].productLine + '</td><td>' + arr[i].demand + '</td><td>' + arr[i].feBug + '</td><td>' + arr[i].bsBug + '</td><td>' + arr[i].bug + '</td><td>' + arr[i].feCode + '</td><td>' + arr[i].bsCode + '</td><td>' + arr[i].feCount + '</td><td>' + arr[i].bsCount + '</td><td>' + arr[i].productScore + '</td><td>' + arr[i].compressibility + '</td><td>' + arr[i].bsCompressibility + '</td><td>' + arr[i].feSubmit + '</td></tr>'
    }
    var htm = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><table style="border-width: 1px;border-color: #666666;border-collapse: collapse;" ><tr><th>id</th><th>productLine</th><th>demand</th><th>feBug</th><th>bsBug</th><th>bug</th><th>feCode</th><th>bsCode</th><th>feCount</th><th>bsCount</th><th>productScore</th><th>compressibility</th><th>bsCompressibility</th><th>feSubmit</th></tr>' + content + '</table></body></html>';
    return htm;
}


var getJSON = function(url) {
    var promise = new Promise(function(resolve, reject){

    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            resolve(body);
        }
    });

  });

  return promise;
};


http.createServer(function (request, response) {
    var str = '11' ;
    response.writeHead(200, {'Content-Type': 'text/html;charset:utf-8'});
    getJSON("http://XXX:8006/api/status").then(function(body) {
        str = handleData(JSON.parse(body).data.status);
        response.write(str);
        response.end();
    });


}).listen(8080);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值