The Server
Every request to your site begins in server/src/Main.gren
.
If you generated your project with prettynice init
, it will look something like this:
If you aren’t familiar with gren, it would be a good idea to read through the book ↗ first. The rest of this guide will assume basic familiarity with gren and focus on the parts specific to prettynice.
Defining your Program
We start with a main
function that defines a SimpleRouter
↗
using defineProgram
↗:
It takes your init
and router
functions, which we will go over next.
Starting your Program
Your init
function should call startProgram
↗.
This is where you define the host and port the server will listen on:
Routing Requests
The heart of your server is the router function. Every request to your site will call this function, with a request ↗ holding information about the request, and a response ↗ that you can use to respond to the request:
request.path
is an array of strings. It holds the path portion of the URL split on /
.
For example, requesting 127.0.0.1:3000/bowties/are/cool
would have a request.path
of ["bowties", "are", "cool"]
.
Requesting the root path would have an empty array ([]
).
You can also match on other things, like the HTTP method ↗:
Take a look at the Request ↗ record and see the pages on pattern matching ↗ and destructuring ↗ in the gren book for help here.
Send HTML
For the homepage, we’re using Response.sendHtml ↗ to render HTML:
Because prettynice needs some control over the <head>
and <body>
tags, you have some fields to inject what you need into the page:
title
: Sets the<title>
tag. Here we’re rendering<title>My Website</title>
head
: An array of HTML that will be injected into the<head>
tag. Here we’re deferring to alinkStylesheet
function to link to a stylesheet. That will be covered in the next section.body
: An array of HTML to make up the<body>
of your page. Here we’re deferring to aviewHomePage
function for this.
Assets and Styling
In the linkStylesheet
function we’re linking to /styles.css
:
styles.css
was created by prettynice init
and placed in the public/
directory.
Any files in public/
will be accessible at the root url path.
So you can put other things there like images, fonts, etc. and reference them in your HTML anywhere you need them.
Initialize Components
The viewHomePage
function returns the HTML for our home page:
We’re referencing a Gen.Components.Counter
in this Html.
Where did this come from?
prettynice init
created a component at client/src/Components/Counter.gren
.
When you run prettynice build
, prettynice looks at all the components in the Components
directory and generates a corresponding module at Gen.Components.[ComponentName]
that lets you initialize the component server-side.
This lets you initialize your client-side components with data from the server.
Here we’re hard-coding that data, but it could just as easily have come from a database, server-side HTTP call, etc.
Learn more about prettynice components in the next section.