javascript - How do I handle POST data in Express.js? -
i consistently getting 500 errors express.js believe hinge on failure obtain string key request hinges on.
on client side, have several requests hitting same point (/restore
), of intended make "key" field in jquery.ajax() call's data
dictionary, included in turn in main dictionary. on client-side, have following, includes localstorage fallback don't think particularly relevant:
var restore = function(key, default_value, state, callback) { populate_state(state, default_value); state.initialized = false; var complete = function(jqxhr) { if (jqxhr.responsetext === 'undefined') { } else { populate_state(state, json.parse(jqxhr.responsetext)); } callback(); state.initialized = true; } jquery.ajax('/restore', { 'complete': complete, 'data': { 'key': key, 'userid': userid }, 'method': 'post', }); if (modernizr.localstorage) { if (localstorage[key] === null || localstorage[key] === undefined) { return default_value; } else { return json.parse(localstorage[key]); } } else { return default_value; } } restore('calendar', default_value, default_value, function() { jquery('#submit-calendar').prop('disabled', false); }); restore('scratchpad', '', result, function() { for(var instance in ckeditor.instances) { if (ckeditor.instances[instance]) { ckeditor.instances[instance].setreadonly(false); } } }); return restore('todo', { 'items': [], 'text': '' }, { 'items': [], 'text': '' }, function() { jquery('#add-activity-button').prop('disabled', false); jquery('#todo-new-entries').prop('disabled', false); }); return restore('youpick', { start_time: new date().gettime() }, { start_time: new date().gettime() }, function() { });
note each call restore() explicitly specifies nonempty, unique, alphabetic string key first argument.
on server side, express's routes/index.js has view servicing request:
router.post('/restore', function(request, response, next) { console.log('router.post /restore'); console.log('query: ' + request.query); console.log('href: ' + sanitize(request.user.href)); console.log('key: ' + sanitize(request.query.key)); var result = look_up_key(request.user.href, request.query.key); console.log(result); response.type('application/json'); response.send(result); });
the sanitize function wipes out characters not alphanumeric or explicitly enumerated punctuation character. should have no request on purely alphabetic key.
this, multiple calls, has output /bin/www of:
router.post /restore query: [object object] href: httpsapi.stormpath.comv1accounts********************** post /restore 500 39.516 ms - 1210 router.post /restore query: [object object] href: httpsapi.stormpath.comv1accounts********************** post /restore 500 5.000 ms - 1210 router.post /restore query: [object object] href: httpsapi.stormpath.comv1accounts********************** post /restore 500 5.842 ms - 1210
it looks there there query, access it? http://expressjs.com/api.html seems should able treat dictionary, among server-side console.log() calls, console.log('key: ' + sanitize(request.query.key));
not appear producing output, empty or corrupt key. appears crash there, apparently sending 500 there.
i probably, or @ least possibly, circumvent issue encoding , decoding data json, , while think that's winning solution, understand why not working.
i don't think key
someone's reserved word; global, hand-inspected search , replace key
identifier
seemed not observably alter behavior.
so 2 questions, in order of preference:
1: how can send , receive variables interpreted putting things or post query string, or taking them out of same? , [object object]
represented request.query?
2: if that's not route take, , should use json, (if anything) should know json encoding in exact context? simple json normally, or there things should advised of?
thanks,
with post
requests, data passed in request body, means need use req.body
instead of req.query
(the latter used access data passed in query string of url). before req.body
works, need include body-parser
middleware.
as why [object object]
being logged: has stringification. log command uses +
"add" string , object (request.query
), use string representation of object , happens [object object
].
instead, should pass objects separate arguments console.log()
:
console.log('query: ', request.query); // comma instead of plus
that show more elaborate string representation of object. alternatively, can have print out json:
console.log('query: %j', request.query)
Comments
Post a Comment