Node Vanilla HTTP Server
Setup
- start server with
node server.js
- run
yarn init
to create package.json (ok to accept defaults) - run
yarn add nodemon --dev
('Node Monitor' package or 'gem') to automatically detect file changes and reboot the server when required. Will only be used in development. Also creates theyarn.lock
file to pin-down versions of each dependency being used and installs all the packages in thenode_modules
folder. - add a script to package.json:
"scripts": { "dev": "nodemone server.js"}
- now run server with
yarn dev
test/1.http
.
Use the REST Client extension for VS Code to test routes and view the raw data returned by the server: Hints
- use
JSON.stringify
to turn javascript (e.g. an array or object) into JSON formatted text
Challenges
- Add /postcode/3021 json response
- Add sendHTML(response, mainHTML) function to utils.js
<h1>About</h1>
<p>This is a paragraph</p>
`)
- Change the not found route to send back JSON instead of plain text, while still keeping the 404 status code — e.g. { "error": "Page not found" }
- Add /assets/main.css route with some simple CSS
- Change sendHTML() to add a to the main.css URL
- Add a assets directory with an gif file inside. Serve that file at /assets/example.gif with the correct content type.
- (Advanced) Add a catch-all /postcode/(number) route, which reads a matching file in a postcodes directory. e.g. /postcode/3040 will read the file at /postcodes/3040.json in the project directory, and send it back. Otherwise, if no matching file is found, it will 404