API 설계 및 구현: xml to json convert

힐링힐링·2023년 11월 29일
0

프로젝트

목록 보기
2/10

Express.js 에서 xml to Json 파싱

코드 부분

xml 데이터를 json으로 변환하여 post


const express = require('express');
const bodyParser = require('body-parser');
const xml2js = require('xml2js');

const app = express();
const port = 3000;

// Middleware to parse XML request body
app.use(bodyParser.text({ type: 'application/xml' }));

// Route to handle XML to JSON conversion
app.post('/convert', (req, res) => {
  const xmlData = req.body;

  // Parse XML to JSON
  xml2js.parseString(xmlData, { explicitArray: false }, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).send('Error parsing XML');
    } else {
      // Send the JSON response
      res.json(result);
    }
  });
});

// Start the server
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

postman

xml데이터를 body에 입력시 json으로 파싱되서 넘어온것을 볼 수 있다,

profile
블로그 이전합니다 https://james-kim-tech.tistory.com/

0개의 댓글