I am trying to find a way to send an Http POST with an array of latency data from a client's html page to the node server on localhost.
So far examples I have found instead show only how to set up the GET/POST request on the server side but not how to send POST request from the client to server.
I have no experience in with http requests so any advice or references anyone recommends?
Just need a push in the right direction :]
My node server needs to be able to receive that array from the client. Here's my server code
var http = require('http')
, connect = require('connect')
, io = require('socket.io');
var app = connect().use(connect.static(__dirname)).use(connect.directory(__dirname));
var server = http.createServer(app);
server.listen(8888);
io = io.listen(server);
Related
I want to have an HTTP server which allows me to establish a TCP connection with a remote computer whenever I specify a given path inside the URL in a browser.
For example... let's say that I have a computer with a public IP address and a hostname as the server (mydomain.com:9000) and a remote computer with a local IP address (192.168.0.1) connected to the server. Somehow I want to establish a TCP connection between a client and the remote computer. And to make it easier just enter an URL with a path (mydomain.com:9000/remote). NOT WITH ANOTHER PORT! Also, I want the connection to be done if and only if that path is entered.
To illustrate a little better how the system works:
_________ _________ _________
| | internet | | local network | |
|_______| ----------> |_______| ----------> |_______|
____|____ <---------- ____|____ <---------- ____|____
client server remote computer
sends http request checks path gets http request
mydomain.com:9000/remote establishes TCP connection handles request
I made some code with node js to make sure that the connection was possible. And it is, now I only have to be able to check the path and establish the connection only then. My code is a simple TCP tunneling proxy and it works fine. But when I try to implement the path it doesn't.
//Import modules
const net = require('net');
const http = require('http');
let inpath = false;
// Create proxy server
const proxy = http.createServer( function(req,res){
console.log(req.url);
//Check path
if(req.url == '/remote')
inpath = true;
});
proxy.on('connection', client => {
if(inpath)
{
const remote = new net.Socket();
//Establish connection
remote.connect(80,'192.168.0.100');
client.on('error', err => {
console.log('Error: ' + err);
proxy.close();
});
//Port-forwarding
client.pipe(remote);
remote.pipe(client);
}
});
proxy.listen(9000);
Is there a way to do this?
In your code, you'll find that the connection event is firing well before your request handler callback is being called. This is because a TCP connection (for which the connection event is triggered) occurs from a client connecting before the client makes its HTTP request. Additionally, what you have isn't really going to work reliably because HTTP keep-alive may mean that multiple requests will come down a single TCP connection.
To fix this, you need to move all of that code you have in your connection event up to your HTTP request handler.
You'll run into other problems though, in that now your code is effectively handling the HTTP request/response, so you'll need to parse and rebuild the request/response data when communicating with the upstream server.
If you truly do wish to handle this at the TCP level, don't use http.createServer(). Use net instead and create a normal TCP server. Then, parse the request data yourself and if you find the certain path you want in the request, make the proxying TCP connection, and be sure to send down the existing buffers so that the upstream server can get the full HTTP request.
I'm trying to do google authentication in my website, where the user gets authenticated on a page. After authentication, it generates a code which contains access_token and refresh_token, and I want to send it to my Node server.
I know Xhttp is a way but I want to avoid that.
So I tried using handle bars. But they only work for html right?
Is there anyway I could use something like helpers to send my code to the server?
I tried to get around some posts like:
How can I share code between Node.js and the browser? (which is quite old, Node must've evolved till then right ?)
Sending data from javascript/html page to Express NodeJS server (I didn't understand this one honestly)
I'm new to Node.js so any guesses or any reference to the docs?
One standard way is setting access token and refresh token on Cookie, so it will be sent with every http request to the backend and can be extracted on server side (node), for example if you use express:
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
this will set req.cookies with an object keyed by the cookie names.
Another option is to use http Authorization header to send the tokens.
I have two subdomains:
socket.mydomain.com - The Socket.IO server
app.mydomain.com - A web app that I'd like to connect to my web socket.
In the landing page for app.mydomain.com, I've linked in the Socket.IO client script, and successfully created an IO object, like so:
<script src=https://socket.mydomain.com/socket.io/socket.io.js></script>
<script type=text/javascript>
const socket = io();
socket.on('message', data => console.log(data));
</script>
However, rather than trying to connect to socket.mydomain.com, the client is trying to connect to app.mydomain.com. Because there's no socket at app.mydomain.com, it fails and keeps retrying.
Is there a way to connect my app at app.mydomain.com to my socket at socket.mydomain.com? Or is this impossible?
Update
Most all of the existing answers relating to this question now use outdated code, because Socket.IO has recently upgraded to 1.0 (1.4 in fact). However, even taking these code changes into account, it seems like Socket.IO doesn't allow what I'm trying to do.
When Socket.IO makes the initial connection to the server, it sends an XHR with the withCredentials settings set to true. But you can't have withCredentials set to true and allow CORS on the server. So it seems my question is actually, 'Is there a way around this problem?'
To accomplish what I wanted, several things needed to be configured correctly, and I needed to use the latest Socket.IO syntax:
CORS must be enabled on the socket.mydomain.com server (Access-Control-Allow-Origin header set to *)
Acceptable transports had to be specified on the server (socket.mydomain.com):
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server, {
transports: ['websocket', 'xhr-polling']
});
The remote server and acceptable transports had to be specified on the client (app.mydomain.com):
const socket = io('socket.mydomain.com', {
transports: ['websocket', 'xhr-polling']
});
I am creating a widget which will update some live NEWS happening. I want to push notification to the widget everytime I get some new information in my database. It is similar to live feed. I am unsure how to start this one in javascript.
I donot want to poll every nth second to get the information. I want the server to push information to the client everytime the server gets a new information.
It is similar to gmail. You get an email even without refreshing the page.
Any pointers will be definitely helpful.
You basically have 2 choices, what I can think of:
Using HTTP, you can ask the server every x second, if it has any new information, and if it does, then load & show it. If you are using HTTP, the server can't contact the client, it can only answer the clients requests.
Using sockets, you can create a 2 way communication, both the client can send data to the server, and the server can send data to the client. This way the server can send the information as soon, as possible. Here you can find information about the supported browsers.
Intermediate Node server can help to separate the business logic from polling operations.
Needs to install these packages - socket.io, express, request
npm install <package_names>
To get request data from node :
var socket = io.connect('http://localhost:3000');
socket.emit('pollNodeServer',JSON.stringify(requestToServer));
On server :
var http = require('http'),
request = require('request'),
app = require('express')(),
server = require('http').Server(app),
io = require('socket.io')(server);
server.listen(3000);
console.log('Server started at port 3000');
var options = {
headers : {
'Content-Type' : 'application/json',
'User-Agent' : 'request'
}
};
socket.on('pollNodeServer', function(data, callbackfn) {
requestServer(data,socket,callbackfn);
});
Reply from server :
socket.emit('returnJobs',JSON.parse(body));
I am new to Node.js and am wondering if anyone has an idea on how I'd go about making a program that sends an OSC message when someone makes an HTTP request to my server?
I have a simple program running right now that when you run it it sends an OSC message, how might I expand it so this message gets sent anytime a certain HTTP request is made?
var osc = require('node-osc');
var client = new osc.Client('127.0.0.1', 3333);
client.send('/1', 1);
Thanks
To receive HTTP requests, you need an HTTP server. Node has a built-in HTTP server. To handle simple routing, I usually go straight for Express which sets up a nice middleware stack and some other helpful bits as you build out your application. From the documentation:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
Now, what I would do is set up a specific endpoint for OSC messages:
app.all('/osc', functin (req, res) {
oscClient.send('/1', 1);
}
You can add parameters and what not if you want Express to handle that for you in the URL, or you can get at the query string or POST data directly from the req object.