我正在为我的朋友们在Restifyapi上建立两个侧面应用程序但是当它开始时 我键入第一条消息,其工作消息发送第二个终端,但当我键入第二个消息服务器终端给出错误
这是服务器:
var restify = require('restify');
var apiai = require('apiai');
var app = apiai("4a8fad8f64874e11bc5d1aa5ea8f96f9");
var chalk = require('chalk')
var sleep = require('sleep')
var clients = require('restify-clients');
const readline = require('readline');
const tls = require('tls');
const server = restify.createServer({
name: 'FridayAI',
version: '1.8.0',
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(chalk.yellow('\nSuccesfull\n'));
console.log(chalk.yellow('Starting on 5 seconds\n'));
server.use(restify.plugins.fullResponse());
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.pre(restify.plugins.pre.context());
function welcome(req, res, next) {
res.send('Welcome')
next()
}
server.get('/s', welcome);
function respond(req, res, next) {
rl.on('line', (input) => {
res.noCache()
res.send(`${input}`);
res.end()
})
}
server.patch('/', respond);
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url , 'Version:' , server.version);
});
这是客户
var assert = require('assert');
var clients = require('restify-clients');
const readline = require('readline');
var chalk = require('chalk')
var restify = require('restify');
var sleep = require('sleep')
const notifier = require('node-notifier');
const path = require('path')
const tls = require('tls');
var client = clients.createJsonClient({
url: 'http://localhost:8080',
version: '1.8.0',
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
client.get('/s', function (err, req, res, obj) {
assert.ifError(err);
console.log(chalk.magenta(obj));
})
client.patch('/', function (err, req, res, obj) {
assert.ifError(err);
console.log(chalk.magenta('FridayAI:', obj));
})
错误:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.noCache (C:\Users\ahmet\Desktop\FridayAI\FridayAPI\node_modules\restify\lib\response.js:91:14)
at Interface.rl.on (C:\Users\ahmet\Desktop\FridayAI\FridayAPI\api.js:51:9)
at emitOne (events.js:116:13)
at Interface.emit (events.js:211:7)
at Interface._onLine (readline.js:282:10)
at Interface._line (readline.js:631:8)
at Interface._ttyWrite (readline.js:911:14)
at ReadStream.onkeypress (readline.js:160:10)
如何在终端发送未经授权的消息,感谢您的帮助
答案 0 :(得分:0)
尝试将功能更改为此
function respond(req, res, next) {
res.noCache()
rl.on('line', (input) => {
res.send(`${input}`);
res.end()
})
}
另外,不确定你使用哪个Restify版本,但你应该"返回next()"最后发送回复;