Greetings. 😃
Sometimes, all we need is an HTML
, a little CSS
, and some JS
— no React circus, no Vite firehose. But we still want our page to reload when we tweak a pixel.
In here, we will use Browsersync 🏆 Node.js package. And of course, you need to install Node.js first on your system.
For code editor, I use VSCode 🏆.
Assuming Node.js is already installed.
To check installed Node.js version on your system, open your terminal (Command Prompt / PowerShell — Windows) then type:
We can employ nvm
to make Node.js installation easier to manage. Available for Windows (nvm-windows) and Linux (default nvm project — for Debian, Arch, Fedora, Slackware, etc. — provided you've got bash
, zsh
, or any POSIX-compliant shell.)
For macOs, we can install default nvm
project — use Homebrew
, add env
to profile.
Setup
Let's do this on Windows 11 operating system — with Node.js is already installed.
-
Open your file explorer, navigate to a folder where your new project folder will reside.
Create a new folder. For example, name it as EXAMPLE.
Let's say the path is
D:\PROJECTS\EXAMPLE
.Navigate to
EXAMPLE
folder in your file explorer ➡️ right click ➡️ Show more options 🤷 ➡️ Open with Code.By using this method, we will directly use VSCode for
EXAMPLE
folder.Now in VSCode, open the bottom panel and go to TERMINAL menu.
We can use
CTRL
+`
(backtick) to open that bottom panel. -
The terminal path would look like this:
I use PowerShell for my terminal in above screenshot.
-
Initialise
package.json
. Type: -
We want to scope the Browsersync package in this current folder only, not globally. Thus, we type:
In that EXAMPLE folder, there will be a new
node_modules
folder and apackage-lock.json
file. -
Create a new, blank
JS
file in that folder. Name itbrowserSync.js
.Use this initial setup (template) for that blank file:
You may change the
port
to be other than 4000.We will use
public
folder for ourHTML
,CSS
, andJS
.A tiddy bit about the
config
variable in that snippet:-
server
➡️ Folder to serve as root directory. -
host
➡️ Hostname to bind the server (usually "localhost" or "0.0.0.0"). -
port
➡️ Port number to serve on. -
files
➡️ Glob pattern of files to watch and reload when changed.A glob is just a fancy developer word for a file pattern — a way to match multiple files or folders using wildcards.
It's like
regex
, but it's not. 😂🤦Glob = Glob...al pattern matching. Global pattern matching. So when we say "glob pattern", it means global pattern matching pattern.
-
-
Create
public
folder in that project. The structure will go like this: -
We then modify our
package.json
.From this initial setup:
To this:
-
The base setup is complete. ☑️
We can run it using:
-
We tweak a bit the
HTML
,CSS
,JS
, and page icon (favicon).
HTML
This is a template for the
HTML
(public/index.html
), so it won't look blank:
CSS
A template for the
CSS
(public/css/index.css
):
JS
JS
(public/js/index.js
):
Favicon
You might want to use your own
.ico
file for thepublic/favicon.ico
.I use this desk lamp icon.
-
Done. ✅
Final look:
Now, you can tinker around with that template. 😃
Every time there's a change in HTML / CSS / JS / other file (anything within
public
folder), it will automatically be synchronised — no need to hit refresh or F5.Hit
CTRL
+C
to stop the "watching" process in the VSCode bottom panel TERMINAL. Or simply close VSCode.For the
JS
generated text, the phrasing, it should be "Served at" rather than "Served on". But hey. 😂 We're on a street corner wearing a trench coat. All of us are jammed into one trench coat. On a street corner. What a sight.
Additional — Full Stack
We can create and run another development server for backend, connect it to a database instance or other endpoint(s) — surely with different port. I always use nodemon for Node.js development — combined with loggers: morgan, winston, and chalk — chalk package is used to colourise the logs.
This is like, this is how we make a cup of tea. Proceeds with bla-bla-bla. And this is how I wash the dishes and measure the mailman's nasal-feature length. No explanation about those.
Folder Structure
The entire folder structure looks like this:
We can add more folders and files to public
folder. 🎻
Why This Matters
🤔 This opening:
Sometimes, all we need is an HTML
, a little CSS
, and some JS
... But we still want our page to reload when we tweak a pixel.
To further elaborate, this technique is useful for:
- Quick prototyping.
- Presenting (teaching) clean file structure.
- Sharing and replicating since no need for complex build tools.
Nice of you to drop in — don't be a stranger now. Ta-ra, be lucky.
Comments
Post a Comment