2

Recently i started programming with Node JS and found it an amazing replacement for php . In php i used to send get requests with Data in the url .

Something like : http://sample.com/public.php?x=helloworld

How to perform something like this in Node JS or is there a better way to send data to node unlike using the url in the above case .

Also , I have noticed that in some cases like stackoverflow , queries are different and dont include the file name

like /public?= instead of /public.php?=

How is this achieved , i always thought this was something related to REST . Also , if you have the answer you might as well guide me if it could be done with Node and a few sources to learn could be of help too .

5
  • The file might be public.php on the server, but it is rewritten using .htaccess (or the alternative for the specific server) Commented Apr 16, 2015 at 11:30
  • possible duplicate of How to get GET (query string) variables in Express on node.js? Commented Apr 16, 2015 at 11:31
  • You can take a look at this Commented Apr 16, 2015 at 11:33
  • Thank you for that , if you could put it in an answer , I'll choose it :) Commented Apr 16, 2015 at 11:44
  • If you've something like location/:city/street in GET endpoint - you can fetch using req.params.city Commented Nov 16, 2018 at 20:16

2 Answers 2

2

the most regular way to use REST api

req.query

// GET /search?q=foo+bar  
req.query.q  
// => "foo bar"  

// GET /phone?order=desc&phone[color]=black&shoe[type]=apple  
req.query.order  
// => "desc"  

req.query.phone.color  
// => "black"  

req.params

// GET /user/william  
req.params.name  
// => "william" 

req.body(for form data)

// POST /login
req.body.username
// => "william"
req.body.password
// => "xxxxxx"
Sign up to request clarification or add additional context in comments.

Comments

1

You'll probably be much better off using a pre-existing module as your web server. You can set one up manually, but you have to know about a lot of potential edge cases and really understand web servers. Most people in node use express. In node, as in any server-side language, you can pass data around in a few ways. The query string is one. You can also put some parameters directly in the url (like "/users/12" where 12 is a user id). Depending on the type of request, you can put data in the body of the request. You can also pass cookies. These are not node-specific. Explaining how express works in a post like this would be crazy, so I'll just give you a short example of a what a route handler matching your example route might look like:

var express = require('express');
var app = express();

app.get('/public', function(req, res, next) {
  // Get the value from the query string. Express makes the query
  // available as an object on the request parameter.
  var x = req.query.x;

  // Execute your main logic
  doSomethingWithX(x);

  // Send a response
  res.status(200).json({ foo: 'bar' });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.