Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

【求助】express下处理2个post请求导致出错,“Can't set headers after they are sent”

$
0
0

逻辑说明:在接收post请求的页面处理两个任务:1. 渲染页面,2. 将post获得的信息传至sae数据库。 如果只处理任务1,没有问题,但是添加了任务2之后,会出现“Error: Can’t set headers after they are sent.”

var express= require(‘express’); var router = express.Router(); var request= require(‘request’);

var recordQuatation =function(param){ var postUrl=“http://sdli.sinaapp.com/”; request.post({url:postUrl,formData:param},function(error,response,body){ if(!error && response.statusCode==200){ console.log(‘send to sae mysql:’,body); }else{ console.log(‘send to sae mysql:’,body); } }); };

//任务1 router.post(’/’,function(req,res,next){ var bodyParse = req.body; recordQuatation(bodyParse); next(); });

//任务2 router.post(’/’,function(req,res){ var bodyParse = req.body; res.render(‘quat’,bodyParse); console.log(bodyParse); });


Viewing all articles
Browse latest Browse all 14821

Trending Articles