Web Server
var express = require('express'),
app = express();
bodyParser = require ("body-parser")
var request = require('request');
//web 폴더 밑에 있는 파일들을 요청이 있을때 접근 가능하도록 합니다.
app.use(express.static(__dirname + '/web'));
app.use(bodyParser.json());
// 유저가 root 를 요청 했을 때, index.html 파일을 전송합니다.
app.get('/', function(req, res) {
res.sendfile('index.html');
});
// 유저가 /get/value 를 요청 했을 때, value 를 전송합니다.
app.get('/get/:value',(req,res)=>{
res.send(`
get value is : `+req.params.value+`
`)
})
// 유저가 /post 로 body 에 value 를 담아 요청 했을 때, value 를 전송합니다.
app.post('/post',(req,res)=>{
res.send(`
post value is : `+req.body.value+`
`)
})
// 유저가 /test?value=1 로 요청 했을 때, 1을 전송합니다.
app.post('/test',(req,res)=>{
res.send(`
value is : `+req.query.value+`
`)
})
// 127.0.0.1 로 get 요청을 합니다.
request('http://127.0.0.1', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
// 127.0.0.1 로 post 요청을 합니다.
request({
url: url,
method: "POST",
headers: {
"content-type": "application/json",
},
json: requestData
// body: JSON.stringify(requestData)
}, function (err, res, body) {
console.log(body);
});
//다른 경로를 요청했을때, 실제 그 경로에 있는 파일을 전달합니다.
app.get('/*', function(req, res) {
res.sendfile(req.url,function(err){
console.log(err);
res.send(403, '잘못된 접근입니다.');
});
});
app.listen(3082); //1024 이하의 포트는 특정 cap 권한이 필요합니다.
0 개의 댓글:
댓글 쓰기