javascript - sock.js not allowing POST method for node server -
trying bring node server uses sock.js websocket communication.
can bring server fine , have established websocket communication. however, need post instance http can send message through websocket. message dependent on post payload...
however, sock.js not seem accepting handler we're creating, , allowing method. causing 405 http code posts done server.
please see following code. if remove sock.js implementation, i'm able process , post requests server.
var app = require('http'); var sockjs = require('sockjs'); var sk = sockjs.createserver({ sockjs_url: '//cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js' }); sk.on('connection', function (conn) { console.log('connection' + conn); conn.on('close', function () { console.log('close ' + conn); }); conn.on('data', function (message) { console.log('message ' + conn, message); conn.write(message); }); }); var server = app.createserver( function(req, res) { if (req.method == 'post') { console.log("post"); var body = ''; req.on('data', function (data) { body += data; console.log("partial body: " + body); }); req.on('end', function () { console.log("body: " + body); }); res.writehead(200, {'content-type': 'text/html'}); res.end('post received'); } else { console.log("get"); res.writehead(200, {'content-type': 'text/html'}); res.end('get received'); } }); sk.installhandlers(server);
i've tried different ways set handlers, including following example here; regardless doesn't seem i'm falling handlers instead sock.js not allowing posts.
if expected behavior sock.js , working way it;s intended to, recommendations use appreciated. attempt move away socket.io because isn't working in cases us... without being able post node server won't able use sock.js either.
you're missing prefix
in call installhandlers
. results in sockjs
handling requests, rather destined it.
just specify prefix:
sk.installhandler(server,{prefix:"/whatever"});
it should of course match prefix use client-side.
alternatively, if don't want use prefix, use different ports.
Comments
Post a Comment