Mastro as a server on the command line
With static site generation (SSG), the whole website is generated upfront. On the other hand, when on-demand rendering or server-side-rendering (SSR), the HTML is generated each time on every request by the server. This comes at the cost of running a server, but enables you to send different pages to different users. When paired with a database like PostgreSQL (and perhaps a query builder like Kysely), Mastro can even serve as a full-stack framework.
In this chapter, you will open a command line interface (CLI for short), and install Mastro as a development server on your laptop.
Different ways to run Mastro
Hereās a table listing the various ways you can run Mastro, with links to more infos.
- Either in your browser (using VSāCode for Web), or by using the command line.
- And either locally (meaning on your laptop) to work on your site, or on a production system in some data center (so users can see your live website).
| Locally | Prod static site | Prod server | |
|---|---|---|---|
| In your browser | Setup | Publish | n/a |
| Command line | Setup | Publish via CI/CD | Deploy server |
Setup local development server
In this section, weāll be using the modern Deno JavaScript runtime, which makes our life a bit easier compared to other runtimes. However, you can also use Mastro with Node.js or with Bun. (In Deno, you donāt need to mess with npm install and node_modules folders, and niceties like TypeScript, Deno.serve, and a formatter are all built-in.)
To preview your website in a browser while you work on it, letās start a local development server.
-
Open a terminal application on your computer, which will provide you with a command-line interface (CLI). VSCode has a terminal built in: to open it, select
View->Terminalin the menu. Alternatively, your OS has a terminal app pre-installed: on macOS under/Applications/Utilities/Terminal, on Windows you can use PowerShell, or for additional compatibility with Linux servers use WSL. -
Install Deno: for example by copy and pasting the following into your terminal:
curl -fsSL https://deno.land/install.sh | shCopied!then hit enter.
-
Navigate to the folder where you want to create your new project folder in, e.g. type:
cd DesktopCopied!and hit enter.
-
Then type (or copy-paste):
deno run -A npm:@mastrojs/create-mastro@latestCopied!and hit enter. This Mastro initalization script will ask you for a folder name for your new project. Enter for example
mastro-testand hit enter (weāre using a dash, because folder names with spaces are a bit of a pain on the command-line). -
Then it will tell you to
cd mastro-test, and from there you can enter:deno task startCopied!This will start your server! You can see the dummy page itās serving by opening the following URL in your web browser: http://localhost:8000 (The
8000is the port. If youād want to run multiple servers on the same machine, each would need to use a different port.)To stop the server again, switch back to the terminal and press
Ctrl-Con your keyboard.
To edit the files in the newly created folder, youāll want to install Visual Studio Code on your computer (or a similar code editor) and open the newly created project folder in it (File -> Open Folder⦠and choose your mastro-test folder).
Check out the contents of the generated folder. Itās a bare-bones Mastro project, but now with:
- a
deno.jsonfile, which specifies the Mastro version to use, and what happens if you typedeno task startordeno task generate, - the
deno.lockfile, which remembers exactly which version of each package was used, - the
server.tsfile, which is executed to start up the server (here you might add customizations like middleware), and - the file in the
routes/folder is now calledindex.server.tsinstead ofindex.server.js, because itās TypeScript ā JavaScript with type annotations. This allowsdeno checkto find certain problems in your code even without running it.
Congrats, youāre all set now to work locally on your project.
Deploy to production
But since your laptop is not always running and connected to the internet, if you want other people on the internet to be able to see it, you need a server in some data center to host your production website.
To deploy your website to production (either by statically generating it, or by running a server in production), see the Mastro deployment docs.
Usually, this involves committing your changes to the Git version control system on your computer, then pushing the commits to the GitHub server, and from there your code will be automatically published to a production web server.
Version control with Git
Just changing your projectās files on your computer will not change them on GitHub. To commit and push your changes, you can either:
- continue to use the Source Control view built into VSāCode,
- install a GUI like GitHub Desktop or Git Up, or
- use
giton the command line, see e.g. this cheat sheet.
Regardless of which interface for Git you use, in the long run it pays off to learn the right mental model for Git.
In the next chapter, letās look at some fun things you can do with a server.