ringo / stick Goto Github PK
View Code? Open in Web Editor NEWJSGI middleware composition layer and application framework
License: Other
JSGI middleware composition layer and application framework
License: Other
Implement a route function to make something like this (new in Express 4.0) possible:
app.route('/myroute')
.all(function(req) {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(function(req) {
return response.json({});
})
.post(function(req) {
// some code
});
This looks like a promising approach instead of cookies for stateless servers:
Use SHA-256 instead.
Line 71 should be;
postParams = JSON.parse(this.input.read().decodeToString(encoding));
instead of;
postParams = JSON.parse(this.input.read.decodeToString(encoding));
it was using read() as an attribute of input, which triggered the following error
TypeError: Cannot find function decodeToString in object function read()
I use Windows 7.
To try stick, I unzipped ringoJS on c:\ringo..then I unzipped stick in c:\ringo\stick.
When I run c:\ringo\stick\examples\demo.js it throws an error that stick [from require("stick")] is not found.
After half hour of blog search and self tricks, I found that you need to do 2 things -
Create a folder named stick under c:\ringo\packages path.
Then move the files under c:\ringo\stick\lib to this c:\packages\STICK directory.
run in DOS CMD below:
ringo -m packages/stick stick/examples/demo.js
Bingo ! Stick worked in localhost:8080
Hannes...Can you please fix the above issue asap ?
Unlike you previous post in several blogs the above manual path issue still exists and needs to be fixed.
Hope this helps many !
Thanks !
jetty-continuation has been dropped . The next stick release will remove the (now legacy) continuation middleware.
In page https://ringojs.org/api/stick/middleware/session/#request.session
the "See ServletSession" is broken.
It links to https://ringojs.org/api/stick/ServletSession/
At the moment it's not possible to render a template without a context object:
app.render("test.html");
This results in "TypeError: Cannot set property "content" of undefined to "WTF?!" (stick/lib/middleware/render.js#52)", which might confuse beginners, who just want to render a basic template without any variables.
The following works:
app.render("test.html", {});
Could not verify token TypeError: Cannot call method "indexOf" of undefined (ringo/utils/strings.js#478)
Script stack:
at ringo/utils/strings.js:478 (startsWith)
at stick/lib/middleware/params.js:145 (anonymous)
I enabled the etag middleware for https://data.seestadt.city and it stopped the response after ~ 30kb.
The static middleware does not support any caching right now. This is problematic for larger files. We should add a general purpose cache / cache header middleware for that, that adds cache headers for the provided content types. This could follow the Apache webserver config approach.
app.cacheControl({
"image/png": { "maxAge": 500, "no-transform": true }
});
I think this is a bug in the reverse lookup with "/". If I change the binding to {action: "/someAction" } it works as expected.
var {Application} = require("stick");
var {redirectTo} = require("stick/helpers");
var log = require("ringo/logging").getLogger(module.id);
var fs = require("fs");
var app = exports.app = Application();
var response = require("ringo/jsgi/response");
app.configure("params", "upload", "route");
app.post("/uploadImage", function(req) {
if (req.postParams.formFileInput && req.postParams.formFileInput.value.length > 0) {
fs.write(module.resolve("files/" + req.postParams.formFileInput.filename), req.postParams.formFileInput.value);
// This redirects to http://localhost:8080/upload/_/_(unknown_route)
return redirectTo(app, {
action: "/" // <----- FAILS with: Error: Unhandled request (stick/lib/stick.js#193)
});
}
return response.html("<html><body><h1>Upload failed.</h1></body></html>");
});
app.get("/", function(req) {
return response.html("<html><head><title>File Upload Demo</title></head> \
<body><form method='POST' action='uploadImage' enctype='multipart/form-data'> \
<h1>Upload:</h1> \
<input type='file' name='formFileInput' /><hr/><input type='submit' value='Upload' /> \
</form></body> \
</html>");
});
Proprosal: Get a reference to the current Application() object in scope using a call similar to Application.currentApp.
Need: This will allow functions which are not part of the normal call flow to get access to the Application object, and thus the JSGI requests object. From here, the current request can be retrieved using Application.currentApp.request.
It would be nice to have the URI template format supported in the route middleware path spec. In other words in addition to "/post/:id" it would be great to also be able to provide "/post/{id}". URI templates seem to be more widely adopted and have the backing of W3C.
What is correct / preferred? Sometimes stick sets e.g. "Content-Type", sometimes "content-type". This should be harmonized since it's a potential source of bugs.
found in
basicauth.js:
return { // Access denied.
status: 401,
headers: {
'Content-Type': 'text/html',
'WWW-Authenticate': 'Basic realm="Secure Area"'
},
body: [
...
error.js:
return {
status: 500,
headers: {"Content-Type": "text/html"},
body: [html]
};
notfound.js:
return {
status: 404,
headers: {"Content-Type": "text/html"},
body: [body]
};
render.js:
return {
status: 200,
headers: {"Content-Type": contentType + "; charset=" + charset},
body: [content]
};
Either use ringo/utils/http.js's Headers utility or always use lower-case keys.
There is an improvement opportunity with the route middleware.
Consider this case:
app.get('/foo/:userid', function() {...});
app.get('/foo/bar', function() {...});
It would be advantageous if a request for '/foo/bar' matched '/foo/bar' instead of '/foo/:userid'.
While playing with writing a lean RESTful API with Stick I've come to add some custom middleware.
Maybe it would make sense to have something like that distributed with core in one way or another.
Please have a look at:
https://github.com/robi42/todos-ng/tree/master/src/main/coffee/middleware
https://groups.google.com/forum/#!topic/ringojs/YZmU1SXGZ4k
Presently, all modules that are mounted using Stick's mount middleware are evaluated prior to routing the request to the module which actually needs to handle a request. For a large site, this could result in a lot of code being evaluated that doesn't need to be. While this only affects the initial request (since the module objects are cached), the cost of those loading requests could be high for large sites.
Stick's mount middleware returns a plain JS response object in case of a missing trailing slash. The current CORS middleware however [relies](https://github.com/ringo/stick/blob/master/lib/middleware/cors.js#L62 et.al.) on the response object's method .addHeaders()
, which leads to an exception. This also happens if using .redirect()
or .notModified()
of the standard RingoJS jsgi utility module, see ringo/ringojs#257.
Probably the easiest fix would be to modify stick to utilize the RingoJS response utility module (there's also a redirectTo()
helper method that would need to change). Otoh it would make sense avoid .addHeaders()
calls, as this won't force applications to use the response util module.
the old String.toByteX is used in quiet a few places
see https://github.com/grob/ringojs/commit/2965bf1f20c02cad55cd9847d42aecb6d34e62f5
Hi,
tried to install stick on ringo under Win XP:
downloaded ringojs-0.7.tar.gz from the ringo website.
I get this log, this is no problem because there is no ringo/jsgi directory:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\ringo\bin>ringo-admin install hns/stick
Downloading http://github.com/hns/stick/zipball/master
Installing package stick
+ C:\ringo\bin\\packages\stick\LICENSE
(... downloads everything ...)
+ C:\ringo\bin\\packages\stick\update-docs.sh
Done
Error: failed to remove file C:\systemp\nils\ringo-install1572099501524327036.zip (fs.js#460)
C:\ringo\bin>cd packages\stick
C:\ringo\bin\packages\stick>ringo demo.js
Wrapped java.io.IOException: Resource "ringo/jsgi/response" not found or not readable (stick/middleware/static.js#12)
at stick/middleware/static.js:12
at stick/middleware.js:46
at stick.js:19
at C:\ringo\bin\packages\stick\demo.js:4
C:\ringo\bin\packages\stick>
downloaded ringo-ringojs-v0.7.0-85-g11170e4.tar.gz from the same website.
I get this log, i do not understand this, because C:/ringo/bin/packages/stick/lib/stick exists:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\ringo\bin>ant jar
Buildfile: C:\ringo\bin\build.xml
init:
compile:
[mkdir] Created dir: C:\ringo\bin\build\classes
[javac] Compiling 48 source files to C:\ringo\bin\build\classes
jar:
[jar] Building jar: C:\ringo\bin\lib\ringo.jar
[jar] Building jar: C:\ringo\bin\run.jar
BUILD SUCCESSFUL
Total time: 2 seconds
C:\ringo\bin>ringo-admin install hns/stick
Downloading http://github.com/hns/stick/zipball/master
Installing package stick
+ C:\ringo\bin\\packages\stick\LICENSE
(... downloads everything ...)
+ C:\ringo\bin\\packages\stick\update-docs.sh
Done
Error: failed to remove file C:\systemp\nils\ringo-install6510812854526382940.zip (fs.js#480)
C:\ringo\bin>cd packages\stick
C:\ringo\bin\packages\stick>ringo demo.js
Cannot find module 'stick' (C:\ringo\bin\packages\stick\demo.js#4)
at C:\ringo\bin\packages\stick\demo.js:4
C:\ringo\bin\packages\stick>
behaves like b)
In short, i was not able to install stick.
Greets, Nils-Hero
If a route is not defined, Stick prints out the following stack trace:
Error: Unhandled request
at stick/lib/stick.js:187 (unhandled)
at stick/lib/middleware/route.js:220 (route)
at stick/lib/middleware/mount.js:125 (mount)
at stick/lib/middleware/params.js:102 (anonymous)
at stick/lib/middleware/upload.js:48 (upload)
at /Users/philipp/Documents/collodion/app/security.js:19 (accessControl)
at stick/lib/stick.js:37 (app)
at ringo/jsgi/connector.js:42 (handleRequest)
It would help to see which route failed. A typical case is "/favicon.ico".
If I have the following two mounts defined:
app.mount('/', module.resolve('test1/config'));
app.mount('/test2', module.resolve('test2/config'));
The first one will always match the incoming url. It is probably best to sort the mounts by least specific prior to matching. Least specific in this case would be determined by the number of slashes in the spec.path. In the above example, the trailing slash is removed from all paths, so the first mount actually becomes "" and the second is "/test2".
It would be nice to have a file upload example to showcase the upload middleware. It's unclear how to use it without looking at the middleware code itself.
I have problem when I tryed to install ringo/scheduler
I saw in version http://ringojs.org/release_0_9 is ringo/scheduler removed
Is there another alternative?
09:58 < Eleeleth> Is there a reason preflights don't immediately return from the middleware?
09:59 < oberhamsi> Eleeleth: hm, good question
09:59 < oberhamsi> i'm investigating now.
10:00 < oberhamsi> i did write a lot of unit tests for cors. wow :)
10:05 < oberhamsi> ok that's a serious bug: preflight request should instantly return. currently it executes the route
10:07 < Eleeleth> yeah. That's the behaviour I was confused about.
10:08 < oberhamsi> yeah, very dangerous behaviour.
My fork of Stick works with Common Node for the most part (while still working with Ringo) but it's rough around the edges. It would be good to come to some agreement on how to address these rough edges, clean up my fork and merge it back in. I'll list the changes I made below:
There's also a few other open issues that I haven't addressed yet.
Any comments or suggestions would be greatly appreciated.
if no Origin header is present, the app returns an error instead of not interfering with the request at all
it has all the info it needs to provide the right response and it's its job to deal with CORS to avoid repeating the same code in the views.
if an application really wants to see the OPTIONS request, we could add a forwardOptions
option to the middleware.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.