In this lab you will build out a server that will provide the details to a pre-built front-end.
-
Create a folder called server
-
Inside the server folder create an index.js file
-
Use npm to install express, then require it at the top of your index.js file
-
Create a variable called app and set its value equal to express invoked
-
Set your server up to accept JSON object responses
-
Set your express server to listen to requests on port 4000, test with nodemon
-
Create a get request for the endpoint ‘/api/users’ and another to get weather information
-
Start your server up and check its functionality using the given HTML file
-
download the materials from Frodo and open the folder in VS Code
-
create a folder called
server
in your lab exercise folder
- inside the
server
folder you just made, create a JavaScript file namedindex.js
-
in order to get our server working we will need to use Node Package Manager to install two packages.
- open up your terminal window and install
express
andcors
using this command:
- open up your terminal window and install
npm i express cors
-
We will need to import the
express
package into ourindex.js
file next- inside
index.js
, importexpress
like so:
- inside
const express = require("express");
-
import the
cors
package next, similar to how you importedexpress
- inside
index.js
, importcors
like so:
- inside
const cors = require("cors");
Cors: Cors is a package that allows the client and server to communicate with each other without the need for advanced configuration.
Next, to avoid repeating lengthy code, we will create a variable called `app that we will reuse to initialize express commands. Set app equal to the invocation of express
- on the next line in
index.js
, set the invocation ofexpress
equal to a variable calledapp
like this:
const app = express();
Our client and server will be communicating by sending and receiving JSON objects to each other. In this step, we will configure our express server to use JSON objects correctly.
- on the next line in index.js, use the
use
method fromexpress
to invokeexpress.json
app.use(express.json());
Similar to Step 5, we will also need to allow our express server to use the cors package we required.
- on the next line in `index.js, allow your express app to use cors functionality like this:
app.use(cors());
Let’s now tell express
to set our server up to run (or “listen”) on port 4000
- on the next line, set your
express
server up to listen to requests onport 4000
app.listen(4000, () => console.log("Server running on port 4000"));
-
Use the nodemon command in your terminal to get your new server listening on port 4000
-
in terminal, use the command to check that your server is working correctly so far
nodemon server/index
We will create first endpoint and method to handle sending a friends array back to the client (front end)
- in
index.js
, above the listen but below any middleware (app.use
calls are middleware), create the follow GET endpoint and method as follows:
app.get("/api/users", (req, res) => {
let friends = ["Nitin", "Eric", "Jeddy", "Cameron", "Riley"];
res.status(200).send(friends);
});
-
Let’s add another endpoint and method that will tell us how the weather is today
-
on the next line, create another GET endpoint and method as follows:
app.get("/weather/:temperature", (req, res) => {
const phrase = `<h3>It was ${req.params.temperature} today</h3>`;
res.status(200).send(phrase);
});
With object destructuring, we can make the above code a little easier to read
app.get("/weather/:temperature", (req, res) => {
const { temperature } = req.params;
const phrase = `<h3>It was ${temperature} today</h3>`;
res.status(200).send(phrase);
});
- Launch the index.html file in your browser and test out your endpoints by clicking the “GET Friends List” button or by navigating to either http://localhost:4000/weather/hot or to http://localhost:4000/weather/cold