美文网首页
Nodejs xml2js模块处理Webservices接口返回

Nodejs xml2js模块处理Webservices接口返回

作者: 报告老师 | 来源:发表于2018-03-15 18:05 被阅读115次
    'use strict';
    // const http = require('http');
    const request = require('request')
    const url = require('url')
    const mysql = require('mysql')
    const express = require('express')
    const parseString = require('xml2js').parseString

    //create dblink
    const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'sjh',
    port: '3306',
    database: 'sjh'
    });

    connection.connect()
    var test = () => {
    return new Promise((reslove, reject) => {
    var address = 'http://ws.webxml.com.cn'
    var paths = ['/WebServices/MobileCodeWS.asmx/getDatabaseInfo']
    var urls = []
    for (var i = 0; i < paths.length; i++) {
    var url = address + paths[i]
    urls.push(url)
    }
    for (var i = 0; i < urls.length; i++) {
    request.get(urls[i], (erro, res, body) => {
    var html = ''
    if (!erro && res.statusCode == 200) {
    reslove(body)
    parseString(body, {
    explicitArray: false
    }, (err, result) => {
    console.log(result.ArrayOfString.string)
    for (var i = 0; i < result.ArrayOfString.string.length; i++) {

          var sql = "insert into html (result) values('" + result.ArrayOfString.string[i] + "')"
            connection.query(sql, (err, result) => {
          if (err) {
            console.log(err)
          } else {
            console.log(result)
          }
          })
          }
          })
        } else {
          console.log(erro)
          reject(erro)
        }
        })
      }
      })
    }
    var app = express()
    app.get('/test', (req, res) => {
      res.setHeader("Access-Control-Allow-Origin", "*")
      res.writeHeader(200, {
      'Content-Type': 'text/xml;charset=utf-8'
      })
      const parseURL = url.parse(req.url, true, true)
      const qqCode = parseURL.query.qqCode
      test().then((data) => {
      res.end(data)
      })
    })

    app.listen(3000, () => {
    console.log('this program is listening on port 3000....')
    })
         

    相关文章

      网友评论

          本文标题:Nodejs xml2js模块处理Webservices接口返回

          本文链接:https://www.haomeiwen.com/subject/wmohqftx.html