Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Mickey Haile -Node-Coursework-Week2 #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mickeyhaile2
Copy link

Volunteers: Are you marking this coursework? You can find a guide on how to mark this coursework in HOW_TO_MARK.md in the root of this repository

Your Details

  • Your Name:
  • Your City:
  • Your Slack Name:

Homework Details

  • Module:
  • Week:

Notes

  • What did you find easy?

  • What did you find hard?

  • What do you still not understand?

  • Any other notes?

Copy link
Member

@JDysiewicz JDysiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Routes look good and functional, some small stylistic changes and some error handling would be nice.

@@ -1,5 +1,6 @@
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nowadays you can just use express.json() (turns out the express package now includes bodyParser by default, and so express.json() is the same thing as bodyParser.json in middleware - this was news to me 😅 )

@@ -20,4 +21,46 @@ app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");
});

app.get("/messages/search", (req, res) => {
const { term } = req.query;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always nice to try and cover off edge cases and handle them; e.g. what if term was an empty string, or undefined? How would you handle that?


app.get("/messages/:id", function (req, res) {
const id = req.params.id;
messages = messages.filter((message) => message.id === Number(id));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To convert to an integer, use parseInt rather than the Number constructor - in this case it'd work out the same, but we tend to prefer parseInt as you can pass in a second parameter to specify the numeral system to parse the string as (e.g. hexidecimal, decimal, oct, etc)

messages = messages.filter((message) => message.id !== Number(id));
res.json(messages);
});

app.listen(process.env.PORT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would add a default value for this in case this env var is not set (e.g. const port = process.env.PORT ?? 3000)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants