Express JS路由器中间件- Body Parser - req.正文有连字符
Express JS Router Middleware - Body Parser - req.body has hyphen
我正在接收来自Mailgun的解析电子邮件,这些电子邮件通过他们的API发布到我设置的URL端点。接收HTTP帖子的URL是使用body解析器中间件的Expressjs到MongoDB的路由。我有http post的路由工作良好的MongoDB简单的文本键,如"发件人",但我有一些包含连字符的消息参数的格式的问题。例如"body-plain"。如果包含"req.body.body-plain"的a参数,Express将抛出错误。有人有办法吗?
我不希望对整个字符串进行正则化。
下面是一个邮件回复的例子:
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 is a test'r'n',
Received:
[ 'from mail-qk0-f179.google.com (mail-qk0-f179.google.com [209.85.220.179]) by mxa.mailgun.org with ESMTP id 556bfda1.7f7658358ef0- in01; Mon, 01 Jun 2015 06:37:21 -0000 (UTC)',
'by qkx62 with SMTP id 62so79143349qkx.3 for <postmaster@appmail.simadsftrade.com>; Sun, 31 May 2015 23:37:21 -0700 (PDT)',
'by 10.140.18.241 with 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 with SMTP id 2mr35323631qkx.33.1433140641295; Sun, 31 May 2015 23:37:21 -0700 (PDT
)',Date: 'Sun, 31 May 2015 23:37:21 -0700'
这是快车路线:
'use strict';
var mongoose = require( 'mongoose' );
var Email = mongoose.model('Email');
module.exports = function(router) {
router.route('/emails')
//creates a 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 DO 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);
});
})
解决方法是使用括号表示法,这样就可以访问包含点表示法无效字符的键
req.body['body-plain']
Extra Details:如果您使用的是mongoose模型,并且它也有连字符,那么您可以这样做。
假设您的猫鼬模型是这样的,请注意名称中带有连字符的属性:
const augmentedWritingTextSchema = new mongoose.Schema({
text: {
type: String,
required: true,
unique: false,
},
name: {
type: String,
required: true,
},
'profile-id': {
type: String,
required: true,
},
'profile-type': {
type: String,
required: true,
},
'company-wide-visibility': {
type: String,
required: true,
},
});
当使用mongoose模型时,你的文档应该是这样的:
augmentedWritingTextDocument = new AugmentedWritingText({
text: reqObj.text,
name: reqObj.name,
'profile-id': reqObj['profile-id'],
'profile-type': reqObj['profile-type'],
'company-wide-visibility': reqObj['company-wide-visibility'],
});
相关文章:
- javascript问题正文样式
- JQuery在单击正文时隐藏上下文菜单
- Expressjs/AngularJS:实现req-flash后出错
- 我可以在网页正文中插入文本链接吗
- 未捕获的类型错误:无法设置属性'innerHTML'如果为null,则将脚本移动到正文不会;不起作用
- 正在尝试将标题标记添加到mailto链接,正文中包含URL
- Node.js req.params.locationid not grabbing ID
- 在外部(正文)单击时关闭弹出窗口
- PassportJS/忘记密码:req.user只定义一次
- 正文中未显示内容
- 将电子邮件正文导出到Google Sheets中的多个单元格中
- req.flash()节点表达式4
- 节点.js快速删除 req.body 不起作用
- 如何从应用于正文的css中排除特定的表单
- 使用 postman.setNextRequest() 在发送之前填充请求正文
- 如何防止sails.js在所有视图中传递req对象
- 阻止在单击和关闭覆盖菜单css时滚动正文
- 在node.jsexpress应用程序中,req.body没有得到<text区域>价值
- 如何从未定义的表单中获取req.body
- Express JS路由器中间件- Body Parser - req.正文有连字符