반응형
특정 경로에 대한 JSON 응답을 제공하는 간단한 코드가 있습니다. 내 현재 코드는 다음과 같습니다.
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: '****',
password: "****",
database: 'restaurants'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 1235);
app.use(express.static(__dirname + '/public/images'));
app.get('/DescriptionSortedRating/',function(request,response){
var name_of_restaurants;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM restaurants ORDER BY restaurantRATING', function(err, rows, fields)
{
console.log('Connection result error '+err);
name_of_restaurants = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'restaurants' : name_of_restaurants
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
위의 JSON과 동일한 XML 응답을 만들려면 어떻게해야합니까?
해결 방법
var xml = require('xml');
response.set('Content-Type', 'text/xml');
response.send(xml(name_of_restaurants));
JavaScript 객체를 XML로 변환하는 방법에 대한 설명은 모듈 설명서를 참조하십시오. 특정 XML 형식으로 반환해야하는 경우에는 물론 더 많은 작업을 수행해야합니다.
참조 페이지 https://stackoverflow.com/questions/21398279
반응형
'MySql' 카테고리의 다른 글
MySQL LOAD DATA INFILE을 사용하여 MySQL 테이블로 가져올 때 CSV 파일의 열을 건너 뛰는 방법은 무엇입니까? (0) | 2020.12.12 |
---|---|
MySQL에서 절차를 호출하는 방법은 무엇입니까? (0) | 2020.12.12 |
MySQL 준비된 진술 및 수레 (0) | 2020.12.12 |
MySQL PDO로 연결 제한 시간 설정 (0) | 2020.12.12 |
MySQL INSERT INTO table IF 테이블이 존재하지 않으면 CREATE TABLE (0) | 2020.12.12 |
댓글