此錯誤:Node.js拋出新的錯誤('發送後無法設置標題'。
{}
_http_outgoing.js:335
throw new Error('Can\'t set headers after they are sent.'); Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.setWriteHeadHeaders (D:\xampp\htdocs\wisaa\node_express\node_modules\morgan\node_modules\on-headers\index.js:80:19)
at ServerResponse.writeHead (D:\xampp\htdocs\wisaa\node_express\node_modules\morgan\node_modules\on-headers\index.js:39:36)
at ServerResponse.writeHeader (_http_server.js:233:18)
at Object.queue.drain (D:\xampp\htdocs\wisaa\node_express\routes\index.js:52:13)
at D:\xampp\htdocs\wisaa\node_express\node_modules\async\lib\async.js:871:23
at D:\xampp\htdocs\wisaa\node_express\node_modules\async\lib\async.js:44:16
at D:\xampp\htdocs\wisaa\node_express\routes\index.js:43:21
at Socket.<anonymous> (D:\xampp\htdocs\wisaa\node_express\node_modules\email-verify\index.js:145:13)
at Socket.emit (events.js:129:20)
npm ERR! Windows_NT 6.2.9200 npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" npm ERR! node v0.12.5 npm ERR! npm v2.11.2 npm ERR! code ELIFECYCLE npm ERR! [email protected] start: `node ./bin/www` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] start script 'node ./bin/www'. npm ERR! This is most likely a problem with the node_express package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node ./bin/www npm ERR! You can get their info via: npm ERR! npm owner ls node_express npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request: npm ERR! D:\xampp\htdocs\wisaa\node_express\npm-debug.log`
這是我的代碼:
var express = require('express');
var router = express.Router();
var _ = require('underscore');
var fs = require('fs'),
async = require('async'),
verifier = require('email-verify');
/* GET home page. */
router.post('/', function(req, res, next) {
var emails = req.body.emails;
emails = emails.split(',');
var queue = async.queue(function(task, callback) {
task(callback);
}, 100);
_.each(emails, function(e, i) {
queue.push(function(cb) {
var done = false;
verifier.verify(e, function(err, info) {
if (err) {
//console.log(err);
// fauile
emails.splice(emails.indexOf(e), 1);
var score = "Your email is nor valid neither exist";
} else {
if (info.success) {
// success
res.json({
'message': emails
});
} else {
emails.splice(emails.indexOf(e), 1);
}
}
if (!done) {
done = true;
cb(null, true);
console.log('something bad happened!');
}
});
});
});
queue.drain = function() {
console.log(arguments);
res.writeHeader(200, {
'content-Type': 'text/plain'
});
res.send(JSON.stringify(emails));
res.end();
}
});
module.exports = router;
這是我的第一個問題,所以謝謝你正確的方式! – Saurabh
如果它看起來適合你,你可以接受編輯。 – guptakvgaurav