javascript - Express JS Router Middleware - Body Parser - req.body has hyphen -


i receiving parsed emails mailgun posted via api url end point set up. url receives http posts expressjs route mongodb using body parser middleware. have http post route working fine mongodb simple text keys 'sender', have issue format of of message parameters containing hyphens. instance "body-plain". express throws error if include parameter of "req.body.body-plain". have work around?

i prefer not regex entire string.

here example of email response being posted:

recipient: 'postmaster@appmail.sisdaf.com',   sender: 'kevsdaf@mail.com',   subject: 'tewsgs',   from: 'kevin psdit <kesdfit@gmail.com>',   'x-envelope-from': '<kasdftit@gmail.com>', 'body-plain': 'this test\r\n',   received:     [ 'from mail-qk0-f179.google.com (mail-qk0-f179.google.com         [209.85.220.179]) mxa.mailgun.org esmtp id 556bfda1.7f7658358ef0-    in01; mon, 01 jun 2015 06:37:21 -0000 (utc)',  'by qkx62 smtp id 62so79143349qkx.3 <postmaster@appmail.simadsftrade.com>; sun, 31 may 2015 23:37:21 -0700 (pdt)',  'by 10.140.18.241 http; sun, 31 may 2015 23:37:21 -0700 (pdt)' ],   'dkim-signature': 'v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com;     s=20120113; h=mime-version:date:message-id:subject:from:to:content-type;   cdx2k5ldwcjwcy0s6407m6/q9tanfiltst48o1ncaczq4rqqyixa vuia==',  'mime-version': '1.0',   'x-received': 'by 10.55.23.130 smtp id    2mr35323631qkx.33.1433140641295; sun, 31 may 2015 23:37:21 -0700 (pdt )',date: 'sun, 31 may 2015 23:37:21 -0700' 

here express route:

'use strict';  var mongoose = require( 'mongoose' );  var email = mongoose.model('email');  module.exports = function(router) {  router.route('/emails')   //creates new email   .post(function(req, res){       var email = new email();  //these work   email.recipient = req.body.recipient;   email.sender = req.body.sender;   email.from = req.body.from;   email.subject = req.body.subject;   email.timestamp = req.body.timestamp;   email.token = req.body.token;   email.signature = req.body.signature;  //these not work because of hyphen in name   email.body_plain = req.body.body-plain;   email.stripped_text = req.body.stripped-text;   email.stripped_signature = req.body.stripped-signature;   email.body_html = req.body.body-html;   email.stripped_html = req.body.stripped-html;   email.attachment_count = req.body.attachment-count;   email.attachment_x = req.body.attachment-x;   email.message_headers = req.body.message-headers;   email.content_id_map = req.body.content-id-map;        email.save(function(err, email) {   if (err){     return res.send(500, err);   }   return res.json(email); });   }) 

the workaround use bracket notation, way can access keys have characters aren't valid in dot notation

req.body['body-plain'] 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -