简体   繁体   中英

Specific url and parameter in Node.js project

I have 2 qeustions : 1. I want my project's url to be like 127.0.0.1:8080/param=id and I couldnt do it, I tried:

app.get('/param=id', function(req, res) {
  console.log(req.param("id"));
});

if I write '/param/:id' it works but I dont want the url to look like this

I want my program to send message according to the id a json message or string to the client

So my second question is how the client gets the response - I want the message to go throgh a script in the client's side?

I would suggest using req.query instead of req.params :

app.get('/', function(req, res) {
  console.log(req.query.id);
  // or you may still use req.param("id")
});

requesting it like

HTTP GET 127.0.0.1:8080/?id=my_id

query is a different way of sending data to the server, designed to send key-value pairs.

Though, if id is the only thing you want to send to the server, I would recommend to stick with params , eg:

app.get('/:id', function(req, res) {
  console.log(req.params.id);
});

requesting it like

HTTP GET 127.0.0.1:8080/my_id

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