Reputation:
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?
Upvotes: 0
Views: 83
Reputation: 966
Since you're using JSON I would use the json
function instead of send
this way your app sends the response with the correct content-type and automatically converts the passed string to a JSON object. The body-parser
also has a function to work with JSON received from the client.
Here's some example code:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/endpoint', (req, res) => {
res.json({
helloWorld: 'hello world!',
msg: req.body.message
});
});
app.listen(3000, () => {
console.log('server listening at port 3000');
});
Old answer
createServer
is deprecated since express 3. You should use the express
function instead.
var express = require('express');
var app = express();
Edit
@robertklep mentioned the express bodyparser is also deprecated.
You can now get this middleware as a separated module via NPM
npm install body-parser
var bodyParser = require('body-parser');
Please visit this link for a list of middleware which are now separated from express itself.
Upvotes: 1
Reputation: 3856
Here is an example from the documentation about how to define routes correctly: express
Reading your question without looking the example it sounded like the actual request to node was failing.
Here is a code block you can start from and build additional routes.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
You can verify routes are working correctly with a browser or a tool like postman
Upvotes: 0