node.js Express with get&post

GET

  1. 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);

    }
    )
  2. req.params.id
    -/localhost:3000/datas/1

1
2
3
4
5
6
7
8
9
10
11
12
app.get('/datas/:id', function(req,res){

var datas = [
'first',
'second',
'third'
];
var rst = datas[req.params.id]
res.send(rst);

}
)

시멘틴 url 을 구글검색해서 잠깐만 보도록!

POST

  1. bodyPaser 모듈 축사
  • npm install body-parser –save
  1. package.json 확인

  2. 소스 추가

    1
    2
    3
    4
    5
    6
    7
    8
    app.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);
    });
  3. req, res

    1
    2
    3
    4
    5
    6
    7
    req
    {
    "title" : "test",
    "description" : "입니다"
    }
    res
    test,입니다