Today's assignment is to build an small API – the server side of a phone book. The API could later be used to build webpages like Eniro or Hitta.se.
We have provided a blob of JSON which will serve as your database for the API. Your task is to create a couple of endpoints in your API;
Method | Path | Description |
---|---|---|
GET | /contacts | Should return all contacts from the JSON blob |
GET | /contacts/:id | Should return only one contact, whose ID matched :id |
If an unknown url is entered (such as /foobar), then the API should respond with a 404 status code. The same goes for if an ID is requested which doesn't exist - it should also return a 404.
🚨 The assignment is only to build the server side, so let's save the frontend for stretch goals. 🚨
In the assets
folder of this assignment you have a contacts.json
file that contains javascript objects of contacts.
Below are the steps we followed in the lecture for creating a new express project. Run all of this from within the code folder of this assignment
Create project & setup dependencies
- If you haven't already, cd into the code folder of this assignment.
- Create server.js. Leave it empty for now.
- Run
npm init
npm install nodemon babel-cli babel-preset-env eslint-config-technigo --save-dev
- create .babelrc:
{
"presets": ["env"]
}
- Create
.eslintconfig.json
:
{
"extends": [
"technigo"
]
}
Make a start script in package.json
{
"scripts": {
"start": "nodemon server.js --exec babel-node",
}
}
Install and configure express
Run npm install express
to install express as a dependency.
Then update server.js to create a server:
import express from "express"
const app = express()
app.get("/", (req, res) =>
res.send("Hello World!")
)
app.listen(3000, () =>
console.log("Example app listening on port 3000!")
)
Change "3000" to something else if you want it to run on a different port.
You can load the contacts.json file into your server.js file using const contacts = require("./contacts.json")
(assuming the contacts.json file is in the same folder as server.js. If it isn't you'll need to use the correct path to the json file).
To test your API, you can use the Postman - a simple HTTP client.
To complete this assignment, you need to fork this repository, add your code in the "code" folder, and then submit a pull request on GitHub (from your repository into the Technigo one) for review.
- Getting started with Express - Hello World example
- Glossary – Talk the backend language
- Sending the first request with Postman
Learning how to think as a web developer is learning how to be an expert in problem solving. So whenever you get stuck start with step 1 and continue until problem solved.
- Google! In English, type in the error message if there is one, search within the language you're using (ie CSS, JavaScript etc).
- Ask your code buddies in your Company.
- Ask your fellow students in Slack.
- Ask Damien. Please note: we are part of a sharing community - share the answer with your fellows.
After completing this assignment your should have tried a little Node.js and Express and have more knowledge about how servers and APIs work.
Done with the main task? Try some of the suggestions below, or make something up yourself.
- Build a frontend which can use the api (create a new react project which runs separately and makes API requests to your server).
- Add on more details to the database to make it more interesting, and add more endpoints which make it possible to find a user by email or phone number, for example.
- Try to make a search endpoint which uses regular expressions to find a user based on multiple fields at the same time, and without having to enter the entire string. For example, so you could search for "technigo.io" and retrieve all users with a technigo.io email address.