node.js Express with get&post
GET
req.query.id
-/localhost:3000/dbs?id=1 (스트링 쿼리)1
2
3
4
5
6
7
8
9
10
11
12
13
app.get('/dbs', function(req,res){
var dbs = [
'first',
'second',
'third'
];
var rst = dbs[req.query.id]
res.send(rst);
}
)req.params.id
-/localhost:3000/datas/1
1 | app.get('/datas/:id', function(req,res){ |
시멘틴 url 을 구글검색해서 잠깐만 보도록!
POST
- bodyPaser 모듈 축사
- npm install body-parser –save
package.json 확인
소스 추가
1
2
3
4
5
6
7
8app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.post('/create', function(req, res){
var title = req.body.title;
var description = req.body.description;
res.send(title+','+description);
});req, res
1
2
3
4
5
6
7req
{
"title" : "test",
"description" : "입니다"
}
res
test,입니다