Is there a way of using relative adress with websockets? - websocket

Is there some easy way of using relative address with websockets? Something like:
WebSocket.listen "<ws://localhost:8080/api>" Msg ->
WebSocket.listen "<ws://api>" Msg
Or do I have to create a task to retrieve window.location.host on start and use it to construct the url?

Related

what is the best way to create rest api to accept html template as an input?

I am using springboot and I need to create Rest APi that will take html template as a parameter. what is the best practice to do that? should I deal with it as #File and save the file in my directory then send it? because I do not need to save the template I just want to send it as email.
Nevermind I dealt with it as string and use Template to create new template with src code, however I had to deal with long string now as source code size could matter later.

path param in URL in GO without any web framework

While developing a REST api in Go, how can we use path params? meaning to say what will be the format of the URI?
http://localhost:8765/myapp/{param1}/entries/{param2}
I tried using something like this to create the route but the handler function is not getting invoked.
Please note that, i intent to use only the net/http package , not any other web framework like gorilla mux.
What I tend to do is nested handlers. "/" is handled by the root handler. It pops the first part of the path, assigns the rest back to req.URL.Path (effectively acting like StripPrefix), determines which handler handles routes by that prefix (if any), then chains the appropriate handler. If that handler needs to parse an ID out of the path, it can, by the same mechansim - pop the first part of the path, parse it as necessary, then act.
This not only has no external dependencies, but it is faster than any router could ever be, because the routing is hard-coded rather than dynamic. Unless routing changes at runtime (which would be pretty unusual), there is no need for routing to be handled dynamically.
Well this is why people use frameworks like gin-gonic because this is not easy to do this in the net/http package IIUC.
Otherwise, you would need to strings.Split(r.URL.Path, "/") and work from those elements.
With net/http the following would trigger when calling localhost:8080/myapp/foo/entries/bar
http.HandleFunc("/myapp/", yourHandlerFunction)
Then inside yourHandlerFunction, manually parse r.URL.Path to find foo and bar.
Note that if you don't add a trailing / it won't work. The following would only trigger when calling localhost:8080/myapp:
http.HandleFunc("/myapp", yourHandlerFunction)

Matching variables with url parameters in nodejs & express

This is really just a conceptual question as I am a beginner to nodejs and express. So basically I would like to have a route that is something along these lines:
Controller
app.get('/explore/:qID?', siteRoutes.explore);
Routes
exports.explore = function(req, res){
...
};
So the way I wold like to have this happen is to pass a variable called qID to the controller using an ajax call. Is there any way that I can take that variable and inject it into the route? My wording is confusing, probably. Basically I want to be able to go to url.com/explore/5 and have that display the corresponding question (qID). Is this the correct way to go about this?
Take a look at req.param in the express docs.
http://expressjs.com/api.html#req.param
basically your route will look like this
app.get('/explore/:qID', siteRoutes.explore);
You can access the qID like this:
var qID = req.param('qID');

Doing a URL rewrite in a ServletFilter using Jetty

I need to do a URL rewrite in a ServletFilter so that "foo.domain.com" gets rewritten to "foo.domain.com/foo". I'm using Jetty, which has a handy way of modifying requests: just cast the request to a Jetty Request object and you get a bunch of setters which allow you to modify it. Here's my code (which doesn't work):
String subdom = Util.getSubDomain(req);
org.eclipse.jetty.server.Request jettyReq = (Request) req;
String oldUri = jettyReq.getRequestURI();
String newUri = "/" + subdom + oldUri;
jettyReq.setRequestURI(newUri);
My purpose is to serve files out of the /foo directory, which lives at /webapps/root/foo.
I'm guessing that I also need to call things like setContextPath(), setPathInfo(), setURI(), setServletPath(), and who knows what else.
What's the magic combination that will make it look like the original request was for /foo?
Edit: to clarify, the reason I say the code doesn't work is that files are still being served out of /webapps/root, not /webapps/root/foo.
Just use the rewrite handler, we have support for what you're trying to do:
http://wiki.eclipse.org/Jetty/Feature/Rewrite_Handler
Answering my own question: I was missing
jettyReq.setServletPath(newUri);
Add that and everything works.

apache mod_rewrite equivalent for node.js?

Is there an equivalent module for Node.js that does what Apache's mod_rewrite does? or is there a standard language construct that provides equivalent functionality?
I'm just getting started with Node and looking to convert my server to this platform.
If you are looking for a good modrewrite library. You might want to look at connect-modrewrite
If you have a HTTP server running with NodeJS you have 2 objects, request and response. The request contains the requested url. Using a require('url') you can parse this requested url and for example get the pathname that's requested.
What you then do with it, is up to your own code obviously. So based on the default example on www.nodejs.org you'd end up with something like this:
var http = require('http');
http.createServer(function (req, res) {
var requestedURL = require('url').parse( req.url );
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write( "You requested " + requestedURL.pathname + "\n" );
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
Which you can test with http://127.0.0.1:1337/foo/bar. Where you can use requestedURL.pathname to determine what you'd want to do, ideally you'd create your own - or use a 3rd party - routing library. They are available, ExpressJS is a pretty famous NodeJS framework which might help take care of a lot of things for you, but I have no experience with it myself.
More information:
Now dead: [http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/]
http://expressjs.com/
As suggested by the previous answers you need to write it yourself; the two previous answers were both more orientated towards handling different paths specially.
You might find my node reverse proxy helpful, as it has a lot of code for handling rewrite rules. This is different than the previous answers because it allows you to match on a rule such as "/people/([a-z]*)" and redirect to "/cgi-bin/index.cgi?user=$1" which is very similar to mod_rewrite.
If you're looking for an equivalent (although not technically, because routers don't actually "rewrite" anything), there are a number of routers out there. Most notably the Connect router (upon which Express is built): https://github.com/senchalabs/connect
It will look something like this:
app.get('/', function(req, res) {
res.end('hello, here is the home page.');
});
It might be better to mess around with the lower-level http interface first though, to get a good feel for it.
There is a rewrite module. And when used with another proxy module in the middleware, they work together as a Reverse Proxy.
I use them while developing Single Page Applications in my local box (so I dont have to configure apache/nginx locally)
This in order to avoid CORS and send all pages (except js/css/images) to index.html for the SPA to work.
var connect = require('connect');
var modRewrite = require('connect-modrewrite');
var proxy = require('proxy-middleware');
var url = require('url');
var app = connect()
.use(modRewrite([
"^\/api\/(.*) /send-to-api/api/$1 [L]",
"^(.*)\/css\/(.*) /send-to-ui/css/$2 [L]",
"^(.*)\/js\/(.*) /send-to-ui/js/$2 [L]",
"^(.*)\/images\/(.*) /send-to-ui/images/$2 [L]",
"^(.*)\/fonts\/(.*) /send-to-ui/fonts/$2 [L]",
"^(.*) /send-to-ui/index.html [L]"
]))
.use('/send-to-api', proxy(url.parse('http://api.server.dev/'))) // Don't forget the last backslash
.use('/send-to-ui', proxy(url.parse('http://ui.server.dev/' ))) // Don't forget the last backslash
.listen(9000)
Check that I use [L] flag because I want it to rewrite and skip the rest of the rules.
In this case, only the /api urls get proxied to api.server.dev, the rest goes to ui.server.dev.
The url prefixes /send-to-api and /send-to-ui are temporary and I use them to differentiate what will go where, it is removed by the connect before sent to their respective servers.
And yes, in case of redirects, proxy-middleware will change the Location header to be localhost:9000

Resources