我正在开发一个简单的节点api / server,它使用websockets,特别是ZeroMQ。但是,在发送/请求数据时,我收到以下错误:
RangeError:无效的状态代码:0在ServerResponse.writeHead(_http_server.js:192:11)
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var server_port = "3000";
/** app settings **/
var zeromq = require("zeromq");
var socket = zeromq.socket("req");/* sends request */
var protocol = "tcp://";
var ip = "192.000.0.000"; //server
var socket_port = "9998";
var url = protocol + ip + ":" + socket_port;
app.use(bodyParser());
/** paths **/
app.post('/request', function (request, response) {
var command = request.body;
//connect to port
socket.connect(url, function (error) {
if (error) {
console.log("connection error : ", error);
process.exit(0);
}
});
//response to front end
socket.send(JSON.stringify(command));
//recieve request
socket.on('message', function (message) {
//output message to console
console.log("Recieved message @ : " + (new Date().toDateString()) + " : " + message.toString());
//send response
response.setHeader('Content-Type', 'text/plain');
response.setHeader('Access-Control-Allow-Origin', 'http://localhost:8888');
response.status(200).send(message);
});
});
/** start app **/
app.listen(server_port);
console.log("Server started on port: " + server_port);
代码更新: 现在我得到了:
错误:发送后无法设置标头。 在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:356:11)
答案 0 :(得分:1)
我猜你正在发送状态0.尝试使用
CREATE INDEX ON :M(Name);