Tuesday 6 June 2017

Retrieving Parameter Sent By Ajax to Node JS

I have a very simple Node JS file:

test.js

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('5');
}).listen(process.env.PORT);  

I am calling this file with a very simple AJAX request from a normal JavaScript file:

getData.js

function testNode(){

 $.get("https://mywebsite.com/test.js?q=Person").done(function(data) {
       $.when(data).then(function() {
           console.log(data);
       });
   });
}

The testNode function as it stands will return that hard coded '5' variable. I'd like to instead have it return the 'Person' string that was in the url of the ajax request.

The person running the IIS server node runs on tells me that Express is installed, yet when I add

var express = require('express'); 

On line two, the application breaks and I get a 500 error.

I have only been working with node a few days, but this is the last piece of the puzzle I need to meet my needs. What am I missing?

I have tried req.query.q and req.params('q') but neither have worked.



via Marcel Marino

No comments:

Post a Comment