Jump to content

jqXHR and express.js and body-parser troubles

prolemur

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 :D heylo

Link to comment
Share on other sites

Link to post
Share on other sites

I don't answer too many topics these days but since you asked so nicely and all.. This might surprise you, but I've done almost no node stuff. I keep meaning to get into it but I don't have the time. So the furst half of that makes little to no sense.

 

I did notice however that how you access the recipes directory seems to be different every single time oh my godwhat a mess. Anyway, javascript handles the way you've specified directories differently as seen here: https://hazy125.com/randomcrap/LTT/dev/lemur/javascriptDirectoryTesting/ such a long URL I should buy a domain just for LTT stuff :/

 

Note that the linkes that are preceded with the slash take me to my sites root directory to look for recipes, so hazy125.com/recipes which sadly doesn't exist whereas links not preceded act in the way I think you're after, where it searches the current directory. So my little to no node knowledge answer would be that I suspect everything related to your node app is at domainroot/recipes whereas your post is trying to send it to... Not there

 

EDIT: http://h125forumhelp.com/lemur/javascriptDirectoryTesting/ That URL isn't any shorter what a waste of money I should just shortlink my stuff

EDIT 2: http://h125.co/ff3 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, Hazy125 said:

-snip-

Lol I did ask so nicely ;)

You must get into node, I've been using it for my webdev class and I think I'll continue to use it later :)

 

I think the error is probably in the node app.post function then since:

$.post('/recipes/', recipeObj);
$.post('recipes/', recipeObj);
$.post('/recipes', recipeObj);
$.post('recipes', recipeObj);

this prints {} 4 times on the server side, meaning all the requests are reaching that app.post statement.

 

Node told me to use / in one of the express tutorials, so ya it's fucked.

It's this damn body-parser module >:(

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×