在 Tink Web 中使用静态中间件
Use Static Middleware with Tink Web
我要申报2条路线。第一个 "/api"
将提供一些 REST 内容,另一个 "/static"
应该提供静态内容。
我尝试从快速入门示例开始,但我不知道该怎么做。
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;
class Server {
static function main() {
var container = new NodeContainer(8080);
var router = new Router<Root>(new Root());
container.run(function(req) {
return router.route(Context.ofRequest(req))
.recover(OutgoingResponse.reportError);
});
}
}
class Root {
public function new() {}
@:get('/')
@:get('/$name')
public function hello(name = 'World')
return 'Hello, $name!';
}
对于 /static
,您可以使用 tink_http_middleware 中的 Static
。
对于 /api
,您可以使用 @:sub
路线。
import tink.http.containers.*;
import tink.http.Handler;
import tink.http.Response;
import tink.http.middleware.Static;
import tink.web.routing.*;
class Server {
static function main() {
var container = new NodeContainer(8080);
var router = new Router<Api>(new Api());
var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
container.run(handler.applyMiddleware(new Static('public_html', '/static')));
}
}
class Api {
@:sub public var api:Root = new Root();
}
class Root {
public function new() {}
@:get('/')
@:get('/$name')
public function hello(name = 'World')
return 'Hello, $name!';
}
首先,我必须使用 tink_http_middleware 的 git 版本和 asys
-lib tink_web
-lib hxnodejs
-lib tink_http_middleware:git:https://github.com/haxetink/tink_http_middleware.git
-lib asys:git:https://github.com/benmerckx/asys.git
-main server.Api
-js www/api/api.js
接下来,@KevinResoL 的回答很有帮助,我只改变了一件事。
staticMiddleware.apply(处理程序)
package server;
import tink.http.Handler;
import tink.http.middleware.Static;
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;
class Api {
public static function main() {
var container = new NodeContainer(8080);
var router = new Router<ApiRoute>(new ApiRoute());
var staticMiddleware = new Static("..","/");
var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
container.run(staticMiddleware.apply(handler));
}
}
class ApiRoute {
public function new() { }
@:sub public var api:Root = new Root();
}
class Root {
public function new() { }
@:get('/')
@:get('/$name')
public function serve(name = 'index.html')
return 'Hello, $name!';
}
我要申报2条路线。第一个 "/api"
将提供一些 REST 内容,另一个 "/static"
应该提供静态内容。
我尝试从快速入门示例开始,但我不知道该怎么做。
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;
class Server {
static function main() {
var container = new NodeContainer(8080);
var router = new Router<Root>(new Root());
container.run(function(req) {
return router.route(Context.ofRequest(req))
.recover(OutgoingResponse.reportError);
});
}
}
class Root {
public function new() {}
@:get('/')
@:get('/$name')
public function hello(name = 'World')
return 'Hello, $name!';
}
对于 /static
,您可以使用 tink_http_middleware 中的 Static
。
对于 /api
,您可以使用 @:sub
路线。
import tink.http.containers.*;
import tink.http.Handler;
import tink.http.Response;
import tink.http.middleware.Static;
import tink.web.routing.*;
class Server {
static function main() {
var container = new NodeContainer(8080);
var router = new Router<Api>(new Api());
var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
container.run(handler.applyMiddleware(new Static('public_html', '/static')));
}
}
class Api {
@:sub public var api:Root = new Root();
}
class Root {
public function new() {}
@:get('/')
@:get('/$name')
public function hello(name = 'World')
return 'Hello, $name!';
}
首先,我必须使用 tink_http_middleware 的 git 版本和 asys
-lib tink_web
-lib hxnodejs
-lib tink_http_middleware:git:https://github.com/haxetink/tink_http_middleware.git
-lib asys:git:https://github.com/benmerckx/asys.git
-main server.Api
-js www/api/api.js
接下来,@KevinResoL 的回答很有帮助,我只改变了一件事。 staticMiddleware.apply(处理程序)
package server;
import tink.http.Handler;
import tink.http.middleware.Static;
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;
class Api {
public static function main() {
var container = new NodeContainer(8080);
var router = new Router<ApiRoute>(new ApiRoute());
var staticMiddleware = new Static("..","/");
var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
container.run(staticMiddleware.apply(handler));
}
}
class ApiRoute {
public function new() { }
@:sub public var api:Root = new Root();
}
class Root {
public function new() { }
@:get('/')
@:get('/$name')
public function serve(name = 'index.html')
return 'Hello, $name!';
}