First application: Hello World
Build a small Mojara web application. You will create it, write the handler, start the server, and change what the browser displays.
1. Create an application
mojara new hello-world web
cd hello-world
The first command creates a web application named hello-world. The second command enters that application.
2. Write Hello World
Open app.mojo and replace the starter code with this example:
from mojara import App, Request, Response
def home(req: Request) -> Response:
return Response.html("<h1>Hello, World!</h1>")
def main() raises:
var app = App()
app.get("/", home)
app.run(port=8000)
What this code does: App creates the web app. home is the function that builds the HTML response. app.get("/", home) connects the root URL to that function. app.run(port=8000) starts the HTTP server.
3. Run the application
mojara dev
Open http://127.0.0.1:8000/ in a browser. The page displays:
Hello, World!
If Mojara prints a different listening port, use that address.
4. Change the response
Edit the HTML inside home:
def home(req: Request) -> Response:
return Response.html("<h1>My first Mojara website</h1>")
Save your changes. Once the development server rebuilds, reload the browser. Your new heading appears.
5. Build and start
Stop the development server with Ctrl+C, then:
mojara build
mojara start
build compiles the application. start starts the compiled server.
Next step
Learn how to add additional pages, serve JSON, and handle POST requests in Routing.