0

I'm trying to make an ajax call on click of a button... But calls are not reaching to node layer. HTML Tag: Show me latest mobile

app.js:

function postDetails() {
    $.post('/api/users', {data: 'blah'}, function (data) {
        console.log(data);
      });
}

server.js

http.createServer(function(req, res) {
    if(req.url == '/') {
        fs.readFile("index.html", function(err, html) {
        res.end(html);
    });
    } else if(req.url.match("\.css$")) {
        var cssPath = path.join(__dirname, 'public', req.url);
        var fileStream = fs.createReadStream(cssPath, 'UTF-8');
        res.writeHead(200, {"Content-Type" : "text/css"});
        fileStream.pipe(res);
    } else if(req.url.match("\.js$")) {
        var cssPath = path.join(__dirname, 'public', req.url);
        var fileStream = fs.createReadStream(cssPath, 'UTF-8');
        res.writeHead(200, {"Content-Type" : "text/javascript"});
        fileStream.pipe(res);
    }

}).listen(8000);


app.post('/api/users', function(req, res) {
    console.log('KKKKKKKKKKuuuuuuuuuuuuuuKKKKKKK');
    router.post('/', function (req, res) {
      res.set('Content-Type', 'application/json');
      var jsonData = JSON.stringify(req.body);
      res.status(201);
      res.json();
    });
});
kk gowtamasa
  • 41
  • 1
  • 9

3 Answers3

1

I think this is because you are creating server and listening requests using http, and trying to handle routes using app (assuming Express object).

So either you should write routing code inside the http.createServer's callback function, like this -

http.createServer(function(req, res){
  if(req.url == '/api/users' req.method === 'POST'){
    //your code here
  }
});

Or you should create the server using app (Express way)

0

Try a simple handler just to verify your assumptions:

console.log('BEGIN');
if (req.method === 'POST' && req.url == '/api/users') {
      console.log('SUCCESS');
    }
console.log('END');
ahoffer
  • 6,347
  • 4
  • 39
  • 68
kk gowtamasa
  • 41
  • 1
  • 9
0

You are not using Node / Express in a good way. If you want to make requests / responses using jQuery and Node.js, you can do it this way :

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

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(express.static('public'));

app.get('/', function(req, res, next) {
  res.sendFile(__dirname + '/index.html');
});

app.post('/api/users', function (req, res, next) {
  res.json(req.body);
});

app.listen(3000, function() { console.log('App running'); });

The code above is a simple node.js server working with express. And below the client page index.html :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>

</head>
<body>
  <button id="clickme">click me</button>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $('#clickme').on('click', function(e) {
      e.preventDefault();
      $.post('/api/users', {data: 'blah'}, function (result) {
        console.log(result);
      });
    });
  </script>
</body>
</html>

This index.html page should be in a public folder. You can refer to another stack overflow question to have more details : here

ElJackiste
  • 4,011
  • 5
  • 23
  • 36