简体   繁体   中英

Nested GET request Nodejs Expressjs

i have a problem with my code. I want to create a nested GET request using Nodejs and Expressjs, i use GET reuqest like:

http://localhost/zigbee/zi?name='Hello'
http://localhost/zigbee/zs?name='Hello'

It is possible to create a main route /zigbee/ e and 2 subroute /zi/ /zs/ ?

I think an implementation like:

app.get('/zigbee/',function(req,res){ ...
     app.get('/zi',function(req,res){ ...
     app.get('/zs',function(req,res){ ...
   }}}

It is possible to do? thanks all

Ew, no. Do this:

app.get('/zigbee/:routeParam',function(req,res){
    var param = req.params.routeParam;
    //do stuff
})

You need to route the request like below

app.route('/zigbee/:action').get(function (req, res) {
  if (req.param('action') == 'zi') {
    // your logic
  }
  if (req.param('action') == 'zs') {
    // your logic
  }
});

You want to create a router and load it in the main app, as shown below.

var zigbee = express.Router()
zigbee.get('/zi', function ...)
zigbee.get('/zs', function ...)

app.use('/zigbee', zigbee)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM