-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (74 loc) · 2.28 KB
/
main.cpp
File metadata and controls
105 lines (74 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <nodepp/nodepp.h>
#include <express/http.h>
using namespace nodepp;
express_tcp_t adminHandler(){
auto app = express::http::add();
app.GET("/:di",[]( express_http_t cli ){
string_t message;
message += "id:" + cli.params["id"] + "\n";
message += "di:" + cli.params["di"] + "\n";
message += "normal method";
cli.send( message );
});
app.GET("/pupu/:di",[]( express_http_t cli ){
string_t message;
message += "id:" + cli.params["id"] + "\n";
message += "di:" + cli.params["di"] + "\n";
message += "pupu method";
cli.send( message );
});
app.GET([]( express_http_t cli ){
cli.send("hello admin!");
});
return app;
}
express_tcp_t normalHandler() {
auto app = express::http::add();
app.GET("/json",[]( express_http_t cli ){
object_t object ({
{ "var1", array_t<uchar>({ 10, 20, 30, 40, 50 }) },
{ "var2", "Hello World! 🫠" },
{ "var3", true },
{ "var4", 1999 }
}); cli.send_json( object );
});
app.GET("/redirect",[]( express_http_t cli ){
cli.redirect("http://www.google.com");
});
app.GET("/send",[]( express_http_t cli ){
cli.send("Hello World!");
});
app.GET([]( express_http_t cli ){
cli.send("hello user");
});
return app;
}
express_tcp_t restFull() {
auto app = express::http::add();
app.GET([]( express_http_t cli ){
cli.send( "Hi! I'm a GET Request" );
});
app.PUT([]( express_http_t cli ){
cli.send( "Hi! I'm a PUT Request" );
});
app.POST([]( express_http_t cli ){
cli.send( "Hi! I'm a POST Request" );
});
app.REMOVE([]( express_http_t cli ){
cli.send( "Hi! I'm a DELETE Request" );
});
return app;
}
void onMain() {
auto app = express::http::add();
app.USE( [=]( express_http_t cli, function_t<void> next ){
console::log( "middle ware" );
next(); });
app.USE( "/api", restFull() );
app.USE( "/admin/:id", adminHandler() );
app.USE( "/user", normalHandler() );
app.USE( express::http::file( "www" ) );
app.listen( "localhost", 8000, []( socket_t ){
console::log( "server started at http://localhost:8000" );
});
}