-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors_example.lua
More file actions
30 lines (23 loc) · 829 Bytes
/
cors_example.lua
File metadata and controls
30 lines (23 loc) · 829 Bytes
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
--- CORS Example
--- Run: lua ./examples/cors_example.lua
--- Test: curl -i -X OPTIONS http://localhost:8082/api/data
--- Test: curl -i http://localhost:8082/api/data
local PudimServer = require("PudimServer")
local server = PudimServer:Create{
Port = 8082,
Address = "localhost",
ServiceName = "CORS Example",
Middlewares = {}
}
-- Enable CORS with defaults (allows all origins)
server:EnableCors()
server:Routes("/api/data", function(req, res)
if req.method == "GET" then
return res:response(200, { msg = "open CORS", origin = "*" })
end
return res:response(405, { error = "Method not allowed" })
end)
print("=== CORS Example (open) running at http://localhost:8082 ===")
print("Try: curl -i -X OPTIONS http://localhost:8082/api/data")
print("Try: curl -i http://localhost:8082/api/data")
server:Run()