Hello friends,
I'm trying to handle a post request to my nodejs express server. It will be sending a JSON object and it should write that to a file. Right now my jqXHR isn't being sent properly, i think...
So here's my code idk where the problem is
/* NODE JS SERVER: */
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var jsonParser = bodyParser.json();
app.use(express.static('assets'));
app.use('/recipes', express.static('recipes'));
app.listen(PORT, () => {
console.log('Express @ localhost:', PORT);
});
app.post('/recipes/', jsonParser, (req, res) => {
console.log(req.body); // this is just an empty object, not the recipe object :(
});
/* JAVASCRIPT / JQUERY: */
$('#submit').click(() => {
var recipeObj = {
"name": $recipe.children('option').filter(':selected').text(),
"duration": $duration.val(),
"ingredients": $ingredients.val().split('\n'),
"directions": $steps.val().split('\n'),
"notes": $notes.val()
};
console.log(recipeObj);
// this console.log work in printing the object
$.post('recipes/', recipeObj);
});
THANKS
PS: I used example code from: https://www.npmjs.com/package/body-parser#express-route-specific https://api.jquery.com/jquery.post/
@Hazy125 heylo