Trying to send the GET http-request. Here is some code:
var http = require('http');
var req = http.request({host: 'testapp.com', path: '/cb.php?a=1&b=2', port: 80},
function (data) {
console.info(data.statusCode)
}
);
req.end();
I can browse url http://testapp.com/cb.php in Chrome, but socket catch timeout error.
What's the reason?
Related
Trying to listen for a websocket event on frontend using the following code
var socket = new WebSocket(`wss://${window.location.host}`);
socket.onmessage = function (event) {
this.step2Text = event.data
}
and firing from backend using
let app = express()
let server = http.createServer(app)
const WebSocket = require('ws')
let wss = new WebSocket.Server({ server });
wss.on('connection', function (ws, req) {
ws.send('some-data')
var stream = new WebSocketJSONStream(ws);
})
the registered function dosen't gets called but says
"Ignoring unrecognized message 'some-data'"
I am emitting messages from socket.io server running on port 8001
but my socket.io client not able to connect and receive these messages
my index.html (client):
<script src="https://cdn.socket.io/socket.io-4.0.0.js"></script>
<script>
//var socket = io();
//var socket = io.connect('http://localhost:8001');
var socket = io('http://localhost:8001', { transports : ['websocket'] });
socket.on('connect', function(){
console.log("connected");
socket.on("message", data => {
console.log(data);
});
});
</script>
My nodejs server code:
const app = require("express")();
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: '*',
}
});
io.on("connection", () => {
console.log("Connected!");
});
var redis = require('redis');
//var url = "redis://:#localhost:6379";
//var redis = require('redis-url').connect();
//var client = redis.createClient(url);
var client = redis.createClient();
//var client = redis.createClient();
client.on("error", function(error) {
console.error(error);
});
client.subscribe('notification');
client.on('message', function(channel, msg) {
console.log("Message received: "+msg);
io.sockets.emit(msg);
});
console.log('starting server on 8001...');
server.listen(8001);
My node js server console logs:
starting server on 8001...
Message received: from laravel
io.sockets.send(msg);
this worked for me. also make sure you are using the same version of socket.io on both client and server
This question has been asked but none of the answers help me figure this out. I'm passing a variable through ajax like so:
var myData = "Hi Og";
$.ajax({
type: 'POST',
data: myData,
url: 'https://example.com:8443',
success: function (data) {
alert(data);
}
});
In my express server I have this in my server.js
var fs = require('fs');
var http = require('http');
var https = require('https');
var bodyParser = require('body-parser')
var privateKey = fs.readFileSync('certificates/key.pem', 'utf8');
var certificate = fs.readFileSync('certificates/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
// your express configuration here
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
// For http
httpServer.listen(8080);
// For https
httpsServer.listen(8443);
app.post('/', function (req, res) {
console.log(req.body.myData);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Content-type', 'text/html');
return res.end('<h1>Hello, Secure World!</h1>');
});
With the above code I get a response back at the originating sender with the alert "Hello, Secure World!". However in the console I get "undefined".
I changed the content-type to application/json but that gave me a 500 error. I changed req.body.data and same thing undefined. req.body gives me this = {}
Per #robertklep direction I needed to property parse the body of the data with app.use( bodyParser.urlencoded() );
I incorrectly had this as json which was causing it to be undefined.
I'm trying to make an https request from my node server to my main php server but it looks like the request isn't going through. I'm pretty sure there is an issue with the hostname line as I have read people having random issues with it but it could be something else. The php server is secured with ssl and is an amazon ec2.
exports.authenticate = function(request, callback) {
var https = require('https');
var options = {
hostname: 'https://mysite.com',
port: 443,
path: '/ajaxim/auth.php',
method: 'GET',
cookie: exports.cookie + '=' + request.sessionID
};
var req = https.request(options)
req.end();
req.on('response', function (response) {
var data = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk;
});
//console.log (request.sessionID);
response.on('end', function() {
try {
callback(JSON.parse(data));
} catch(e) {
callback();
}
});
});
};
Any suggestions will be insanely helpful. I'm just trying to get hint of the right direction.
I used Node-http-proxy to proxy my request from port sample.com:80 to port sample.com:8080 with sample below:
var httpProxy = require('http-proxy');
httpProxy.createServer(8080, 'localhost').listen(80);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
When I do: curl with POST to sample.com:80, I get 403 Forbidden response. But when I do curl with GET, 301 permanent redirect. My question is:
Is it possible to make POST and GET behaves the same, which always return 200 status code instead of 403 or 301?
Thanks
var fmt = require('fmt');
var httpProxy = require('http-proxy');
fmt.sep();
console.log();
fmt.title("\033[1;36mPROXY SERVER STARTED\033[0m");
console.log();
fmt.field("\033[1;34mPROXY PORT\033[0m", 80);
fmt.field("\033[1;34mTARGET PORT\033[0m", 8080);
fmt.field("\033[1;34mTARGET ADDRESS\033[0m", '127.0.0.1');
var server = httpProxy.createServer(function(req, res, proxy)
{
proxy.proxyRequest(req, res, { host: '127.0.0.1', port: 8080, buffer: httpProxy.buffer(req) });
var reqBody = '';
req.on('data', function(chunk)
{
reqBody += chunk;
});
req.on('end', function()
{
console.log();
fmt.sep();
console.log();
fmt.title("\033[1;36mREQUEST FROM SHARED CLOUD\033[0m");
console.log();
fmt.dump(req.connection.remoteAddress, "\033[1;35mADDRESS\033[0m");
console.log();
fmt.msg("\033[1;35mHEADERS\033[0m :");
fmt.dump(req.headers);
console.log();
fmt.msg("\033[1;35mBODY\033[0m :");
fmt.dump(reqBody);
console.log();
});
});
server.proxy.on('proxyError', function(err, req, res)
{
fmt.title("\033[1;36mPROXY STATUS\033[0m");
console.log();
fmt.dump('failed to proxy on port ' + 8080, "\033[1;31mERROR\033[0m");
res.end();
});
server.proxy.on('end', function()
{
fmt.title("\033[1;36mPROXY STATUS\033[0m");
console.log();
fmt.dump('request was proxied successfully', "\033[1;33mSTATUS\033[0m");
});
server.listen(80);