Coding ยท ยท

Your first website

You'll be creating a static website from the very basics, running a local server and then using Git as version control. Your website will be deployed to a subdomain of compsci.me.

Anatomy of a web page

When you visit a web page, a request is made to a server which sends an HTML response. That HTML response may then trigger more requests, for example for CSS, JavaScript, images and other files.

In this guide we'll only be using HTML and images, adding CSS and JavaScript later.

Your website's folder

In ~/code make a new folder called website with a subfolder within it called public. All the web pages (and CSS, JS, images, ...) you make for your website which you want to be accessible in a web browser need to go in that public folder. Later in the course, outside that public folder (but inside the website parent folder) you'll be creating server-side code that you want to keep private. In addition, you may want to put a README.md in that website folder now, perhaps to keep track of tips and tricks and things to remember for yourself.

Starting and editing web pages

Our general workflow when creating web pages:

Web pages should start with <!doctype html> to tell the browser you know what you're doing, and they're also required to have a <title>Title</title>.

A very basic template to start from:

<!doctype html>
<title>My first web page</title>
<h1>Hello world</h1>
<p>Hi!</p>

For quick pages I don't bother with <html>, <head> and <body> tags as they get auto-added for you (but you need to know them for exams).

Run a server locally

You can just make a file and click it to open it in a web browser (if you haven't already, try that with the template above). If you do this, the URL in the web browser will be eg file:///C:/Users/jake/page.html. But URLs that start file:// aren't ideal, as they won't behave the same as real websites.

Instead, we'll create a local web server which can be accessed at URLs starting http://.

Vite will serve all files in the current folder, including automatic browser reloading whenever you save your HTML.

Because Vite automatically reloads, you no longer have to ALT+TAB and CTRL+R when you save a file.

Option 2: Using Bun's static site server

This should be the quickest and simplest option, but a downside is that it doesn't serve all files, just the HTML files, and it serves them at slightly different URLs/paths. Just run:

bun *.html

... and Bun will serve all files with the .html extension. It does other clever stuff as well like find linked CSS and images, and it automatically reloads in the browser whenever you change a file (saving you from ALT+TAB and CTRL+R).

Word of warning though: it doesn't serve up all files in the folder, which can sometimes cause problems. For that, use option 1 above instead with Vite.

Put index.html files in folders

When you run a server (like Vite above), when it receives a request to / it will usually look for a file called index.html and respond with it (or respond with 404 if not found). If you have a file cat/index.html then someone can access it just by going to /cat/. It's wise to always have an index.html file in any folder, especially your main folder.

Basic HTML to learn first

Start by checking you can use:

<h1>Different</h1> <h2>level</h2> <h3>headings</h3>,
<p>paragraphs</p>,
<div>block containers</div> and
<span>inline containers</span>.

Sprinkle some <b>bold</b> and <i>italic</i> text around too.

You need to know about <a href="./page2.html">links</a> and <img src="cat.jpg"> images.

Note that all the tags above have a matching closing </tag>, except for the image <img> which is self-closing.

Try putting an image in your folder and create a second HTML page and make it so you can click an image and it takes you to the other page, which also has a link back to the first page.

Spend a bit of time playing around with these tags and trying to create 2 or 3 very basic web pages that link together. You may want to use the HTML guides on MDN or W3Schools to help.

Using version control with your website

Version control tools such as git allow us to keep track of changes to files/projects, saving a complete history of who changed what and when. GitHub is a company (now owned by Microsoft) which provides a platform for keeping track of your git projects and much more.

Create a ~/.gitconfig

Setting up your repo

You're going to turn the simple website you've just made into a git repository, which you'll then track on GitHub:

Making changes in your repo

Future changes

Whenever you make changes in the future, at a minimum you'll need to do git commit -am 'whatever' and git push. I also recommend running git status and git diff before.

When you add new files, you'll need to run git add . (or git add [filename]) to tell git you want to track them, before you do commits/pushes.

If you want to have the same repo on another device, git clone [repo] on that device.

If you edit a file directly on GitHub.com, or on a different device, you should git pull those changes.

Putting your website online

Mr Gordon will put your website online, on a server he manages. We have a home page at https://compsci.me/ which will include a link to your website.

Future deploys

To deploy a new version of your website, just make a change to it and commit and push your repo to GitHub. It usually deploys in under 5s.

Optional: putting a website online yourself

NOTE: this is optional as Mr Gordon will put your website online, but you may like to try this for any other website you want to use.

See the legacy file for other ways to get your website online yourself. Some are free, some cost a few quid a month. The best is probably with Deno Deploy.