Hi Im building two side app on Restifyapi for my friends but when its started i typed first message and its works message sended second terminal but when i typed seccond message server terminal gives error
This is server :
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);
});
And This is client
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:
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)
How can I send unliminited message on terminal thanks for help
try changing the function to this
function respond(req, res, next) {
res.noCache()
rl.on('line', (input) => {
res.send(`${input}`);
res.end()
})
}
also, not sure of which Restify version you use, but you should "return next()" at the end to send the response;