How to implement and use HTTP/2 in Node.js - javascript

I have a node.js app. I was using http for creating server as shown below:
...
var http=require('http');
var server=http.createServer(app);
...
And this works perfectly.
Then I implemented http/2 module from npm as shown below :
npm install http2
And I have the package now. Then I changed my code as:
var http2 = require('http2') ;
var server= http2.createServer(app);
But this doesn't work. I get "could not get any response" error from postman.
Why doesn't it work and how can I fix it and use http2?
EDIT : I found HTTP/2 documentation. You can see how you can implement HTTP/2, what you should do for server side, what you should do for client side and other informations about http2 in this document. Also I found out that to check http2 with browsers, you need to generate the certificate and key in this document. The document:
https://nodejs.org/api/http2.html#http2_server_side_example

Postman does not support HTTP/2.
Source: https://github.com/postmanlabs/postman-app-support/issues/2701

Related

web gRPC with TLS on localhost

I have a Go server and client using a local trusted certificate and they communicate flawless with each other. Now I want the Go server to communicate with a web-grpc instance too. Insecure didn't work as browsers force HTTP2 to go over TLS or deny it fully. And after all; it should work on TLS in production too anyway. Another issue is CORS too, which I can't figure out yet to give to https://github.com/improbable-eng/grpc-web 's version of the server implementation to add origin headers. But first things first.
I serve a simple HTML and Webpack build JS that I serve with a simple Golang FileServer over TLS.
I first generated a new TLS cert/key (that the already-working Go Server/Client pair use successfully):
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=www.wp-ts.loc" -keyout ./www.wp-ts.loc.key -out ./www.wp-ts.loc
Then I added it to the macOS keychain to be trusted:
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain www.wp-ts.loc
This is the Protobuf file I use:
syntax = "proto3";
package pb;
service GCDService {
rpc Compute (GCDRequest) returns (GCDResponse) {}
}
message GCDRequest {
uint64 a = 1;
uint64 b = 2;
}
message GCDResponse {
uint64 result = 1;
}
Then build with:
protoc -I=. gcd.proto \
--js_out=import_style=commonjs:. \
--grpc-web_out=import_style=commonjs,mode=grpcwebtext:.
This is the simple HTML page I serve:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>gRPC</title>
<script src=".//dist/main.js"></script>
</head>
<body>
</body>
</html>
Then the JS module:
import { grpc } from "grpc";
import { GCDRequest, GCDResponse } from "./gcd_pb.js";
import { GCDServiceClient } from "./gcd_grpc_web_pb.js";
var root_certs = ["www.wp-ts.loc", "www.wp-ts.loc.key"];
var sslCredentials = grpc.credentials.createSsl(...root_certs);
var client = new GCDServiceClient("https://www.wp-ts.loc:3000", sslCredentials);
var request = new GCDRequest();
request.setA(294);
request.setB(462);
client.compute(request, {}, (err, response) => {
console.log(err);
console.log(response);
// console.log(response.getResult());
});
Which is build with Webpack like so (which outputs to ./dist/main.js and thus read by the index.html):
npx webpack client.js
The www.wp-ts.loc test domain is in my /etc/host so it can mimic as a domain with the certificate and reroute all traffic to localhost.
Now here's the issue I can't really seem to find in all the big library overhead as this mainly seems to be meant for NodeJS. The Webpack builder with new GCDServiceClient() without credentials builds fine, but of course the browser won't allow the non-TLS style (leaving CORS out of the equation for now). Then using the credentials as a test (which of course is dangerous, but I'm trying out along the way and can't find good docs for it on grpc-web style) gives the obvious NodeJS issue that it can't use the filesystem:
... // Many lines here, but this is clear enough I guess
ERROR in ./node_modules/grpc/src/grpc_extension.js
Module not found: Error: Can't resolve 'fs' in '/Users/#USERNAME/Downloads/wp-ts/node_modules/grpc/src'
# ./node_modules/grpc/src/grpc_extension.js 34:11-24
# ./node_modules/grpc/index.js
# ./client.js
Maybe I'm just approaching this totally wrong and I'm also aware that the grpc-web implementation is still in a very brittle phase, but how do you make it work to connect correctly over HTTP2/TLS (with certificate) and maybe if known to get the CORS headers to be added to https://github.com/improbable-eng/grpc-web 's server that I add to my Listener in Go at port 3000.
Sorry for big gist, but I hope someone can help me with this. I'm really excited to get going with Go + Browser gRPC/Protobuf :D
Thank you so much in advance!
you need a grpcwebproxy for forwarding your browser request to real grpc server.
https://github.com/improbable-eng/grpc-web/tree/master/go/grpcwebproxy

WebSocket client can't connect to cloud server through corporate proxy except for web browsers! (ETIMEDOUT)

I'm trying to connect a WebSocket client (corporate Node.JS server) to a Cloud server, but it times out (connect ETIMEDOUT error).
I'm not sure which needs configuration... Linux, Node.JS or the WebSocket Client?
I've already configured the proxy for Linux (export proxy/https_proxy), and for Node.JS (npm config set proxy/https_proxy) but the problem persists!
I'm using the ws library found in https://npmjs.com and implemented it without any sort of options except setting port.
Any advice?
EDIT: Works fine in the web browser, by the way.
node does not automatically use any form of configuration for making HTTP requests through proxies -- ie, node does not read the PROXY or HTTPS_PROXY environment variables. npm config set proxy only affects npm itself.
In other words, node programs always try to access servers directly. It appears your network requires HTTP requests to go through a proxy, and direct connections are being silently dropped.
If you want your program to make HTTP requests through proxies, you must do so manually. The http-proxy-agent module can help you do this.

require is not defined on Spotify authorization code

We are doing a web using HTML and JavaScript. We have some problems when trying to do the authorization process to connect to the Spotify API.
var express = require('express'); // Express web server framework
var request = require('request'); // "Request" library
var querystring = require('querystring');
var cookieParser = require('cookie-parser')
This code we found on the internet uses require() to get some data but Chrome says:
Reference error: require is not defined.
We found that the JavaScript node may be the problem so we installed npm and Browserify but is not working.
We also used this in our HTML:
<script type="text/javascript" src="node.js"></script>
<script type="text/javascript" src="js/main.js"></script>
HELP??
What you have is the code that suppose to run on the server side. Node, Express, are server side components, and you can't run them on the clients side (inside of the browser). Server side is using 'require' to manage and load modules, that is why you have require there.
I think what should you do is to send your request to the server and server should process authentication code that you have there and send response back with the status if user authenticated or not.
require() is not an existing method for client-side JS.
Use a <script> tag then call the method after it.
This was already answered here:
Client on node: Uncaught ReferenceError: require is not defined\
Node.JS is a serverside application and you are attempting to run this clientside.
Please try using and referring to this http://requirejs.org/

Is it possible to set up a socket.io client running (server-side) on a node.js server?

I'd like to enable socket-based p2p communications between two or more different node.js application servers. I'm using socket.io to handle all such communication between a given server and the web application it serves - but what I'm looking for is a way to communicate server-to-server.
I had initially assumed it would be as easy as something like this:
var io = require("socket.io");
var socket = io.connect("my remote endpoint");
However, as it turns out the server-side socket.io implementation doesn't offer a "connect" method, only a listen method.
Why is this? Why can't I treat a node application server as a client to a socket.io server running elsewhere? Is there any way that I can achieve this functionality?
OK, so thanks to #pimvdb in the comments above I've got a workable solution.
Basically, the socket.io library that npm installs has a dependency on another module, called socket.io-client. In a standard socket.io installation this will be installed in node_modules/socket.io/node_modules/socket.io-client
However, it's also possible to say "npm install socket.io-client" and install it as its own first-class citizen library.
Then your usage looks like this:
var client = require("socket.io-client");
var socket = client.connect("http://myendpoint.com:3000/whatever");
socket.emit("test", "foo");
And everything works.
So, thanks man!
Just for clarification, this is an example with listeners and possibility to emit events (and without install again a module already installed)
var io = require('socket.io/node_modules/socket.io-client');
client = io.connect('http://'+CONFIG.host+':'+CONFIG.port);
client.on('connect',function() {
client.emit("test","foo");
});
Before you go full speed on socket.io for server-to-server communications.....
socket.io is engineered as a browser to server comm infrastructure. I'm far from certain it is the best solution for P2P server stuff. Plus, if you do server-to-server - why not just do Websockets? There are various websocket modules for node - e.g. https://github.com/einaros/ws

Can a proxy (like fiddler) be used with Node.js's ClientRequest

Can node.js be setup to recognize a proxy (like Fiddler for example) and route all ClientRequest's through the proxy?
I am using node on Windows and would like to debug the http requests much like I would using Fiddler for JavaScript in the browser.
Just be clear, I am not trying to create a proxy nor proxy requests received by a server. I want to route requests made by http.request() through a proxy. I would like to use Fiddler to inspect both the request and the response as I would if I was performing the request in a browser.
I find the following to be nifty. The request module reads proxy information from the windows environment variable.
Typing the following in the windows command prompt, will set it for the lifetime of the shell. You just have to run your node app from this shell.
set https_proxy=http://127.0.0.1:8888
set http_proxy=http://127.0.0.1:8888
set NODE_TLS_REJECT_UNAUTHORIZED=0
To route your client-requests via fiddler, alter your options-object like this (ex.: just before you create the http.request):
options.path = 'http://' + options.host + ':' + options.port + options.path;
options.headers.host = options.host;
options.host = '127.0.0.1';
options.port = 8888;
myReq = http.request(options, function (result) {
...
});
If you want to montior outgoing reqeusts from node
you can use the request module
and just set the proxy property in the options, like that
request.post('http://204.145.74.56:3003/test', {
headers :{ 'content-type' : 'application/octet-stream'},
'body' : buf ,
proxy: 'http://127.0.0.1:8888'
}, function() {
//callback
});
8888 is the default port , of fiddler .
process.env.https_proxy = "http://127.0.0.1:8888";
process.env.http_proxy = "http://127.0.0.1:8888";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
Answering my own question: according to https://github.com/joyent/node/issues/1514 the answer is no, but you can use the request module, http://search.npmjs.org/#/request, which does support proxies.
If you want to configure a proxy in the general case, the other answers are right: you need to manually configure that for the library you're using as node intentionally ignores your system proxy settings out of the box.
If however you're simply looking for a fiddler-like HTTP debugging tool for Node.js, I've been working on an open-source project to do this for a little while (with built-in node support) called HTTP Toolkit. It lets you
Open a terminal from the app with one click
Start any node CLI/server/script from that terminal
All the HTTP or HTTPS requests it sends get proxied automatically, so you can see and rewrite everything. No code changes or npm packages necessary.
Here's a demo of it debugging a bunch of NPM, node & browser traffic:
Internally, the way this works is that it injects an extra JS script into started Node processes, which hooks into require() to automatically reconfigure proxy settings for you, for every module which doesn't use the global settings.

Categories

Resources