After looking around google and stack overflow, I'm yet to find a way to post data to my Node.js server.
There seems to be disparity between the methods I've seen, presumably due to depreciation.
One method I saw was this code:
var express = require('express');
var app = express.createServer();
app.use(express.bodyParser());
app.get('/endpoint', function(req, res){
var obj = {};
obj.title = 'title';
obj.data = 'data';
console.log('params: ' + JSON.stringify(req.params));
console.log('body: ' + JSON.stringify(req.body));
console.log('query: ' + JSON.stringify(req.query));
res.header('Content-type','application/json');
res.header('Charset','utf8');
res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');
});
app.post('/endpoint', function(req, res){
var obj = {};
console.log('body: ' + JSON.stringify(req.body));
res.send(req.body);
});
I get the error:
TypeError: express.createServer is not a function
at Object.<anonymous> (c:\Users\Tobi\Desktop\ChangeLog\ChangeLog\app.js:2:19)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
I did some reading and saw that Express had stopped coming with "middle-ware" and they would need to be included seperately - I also got an error message saying so.
What is the current / best way to implement Ajax Post requests to my server?