On this page
O Language includes an HTTP server in its runtime. Pass a port and route table to webserver. A route response can be a string or a function.
def routes = {
"/": {"type": "GET", "response": "Hello from O Language"}
}
webserver("10000", routes)
A function response can use route parameters and the request-scoped server value. The server exposes request headers, query values, form values, route parameters and response settings to the handler.
def hello = fn(name) {
return json({"message": "Hello, " + name})
}
def routes = {
"/hello/:name": {"type": "GET", "response": hello}
}
webserver("10000", routes)
The route parameter is available to the handler and as server.parameters. The request also exposes query data as server.get, form data as server.post and request headers as server.headers.
Choose how to build the application
- Use
webserverdirectly for a small program with a short route table. - Use the routing guide when mapping methods, paths and handlers.
- Use database APIs for persistence.
- Use the O Language Web Framework when you want the
olang webproject layout and component commands.
The built-in server binds to a network port. Review the listening address, handlers, upload configuration and response settings before exposing an application.