逻辑说明:在接收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); });