본문 바로가기

Node.js

Web에서 node로 구성된 서버로부터 정보 가져오기

*HTML side Source Code

var xmlHttp = null;
xmlHttp = new XMLHttpRequest();

xmlHttp.open("POST", '/setRelay', true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

//Form Data 형식으로 전송
xmlHttp.send("channel="+_ch+"&status="+value);

 

var result= JSON.parse(xmlHttp.responseText);

 

 

 

 

*Node.js Server side Source Code

//여기서 가장중요한건

var bodyParser  = require('body-parser');

app.use(bodyParser.urlencoded({extended:false}));

bodyParser를 적용하지 않으면 아래의 req.body가 undefine으로 나온다.

결국 접근이 안됨.

 

 

app.post('/setRelay', function(req, res){
var channel = req.body.channel;
var status = req.body.status;
var ch = "CH"+channel;

var query = '$RPATH/onoff '+ch+' '+status;
chile = exec(query, function(err,stdout, stderr){
if(err == null)
{
console.log(query);
res.send({'status' : 'OK'});
}
});

 

'Node.js' 카테고리의 다른 글

git 계정 변경  (0) 2015.09.23
IP Address 유효성 체크  (0) 2015.08.14