-
-
Notifications
You must be signed in to change notification settings - Fork 60
NW | 26-SDC-Mar | Zabihollah Namazi | Sprint 3 | Middleware #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZabihollahNamazi
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
ZabihollahNamazi:middleware-sprint3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c7ed9a
gitignore
ZabihollahNamazi db9653b
first commit adding files and directories
ZabihollahNamazi 25b3feb
app and middlewares
ZabihollahNamazi 48a4cdb
update the app.js
ZabihollahNamazi d7cf339
adding files directories
ZabihollahNamazi b553fd4
updates
ZabihollahNamazi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import express from "express"; | ||
| import usernameMiddleware from "./middleware/usernameMiddleware.js"; | ||
| import jsonArrayMiddleware from "./middleware/jsonArrayMiddleware.js"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.post("/", usernameMiddleware, jsonArrayMiddleware, (req, res) => { | ||
| const subjects = req.body; | ||
|
|
||
| const auth = | ||
| req.username | ||
| ? `You are authenticated as ${req.username}.` | ||
| : "You are not authenticated."; | ||
|
|
||
| let message = ""; | ||
|
|
||
| if (subjects.length === 0) { | ||
| message = "You have requested information about 0 subjects."; | ||
| } else if (subjects.length === 1) { | ||
| message = `You have requested information about 1 subject: ${subjects[0]}.`; | ||
| } else { | ||
| message = `You have requested information about ${subjects.length} subjects: ${subjects.join(", ")}.`; | ||
| } | ||
|
|
||
| res.send(`${auth}\n\n${message}`); | ||
| }); | ||
|
|
||
|
|
||
| app.listen(4000, () => { | ||
| console.log("Server running on 4000"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| export default function jsonArrayMiddleware(req, res, next) { | ||
| const chunks = []; | ||
|
|
||
| req.on("data", (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on("end", () => { | ||
| const bodyString = Buffer.concat(chunks).toString(); | ||
|
|
||
| let data; | ||
|
|
||
| try { | ||
| data = JSON.parse(bodyString); | ||
| } catch { | ||
| return res.status(400).send("Invalid JSON"); | ||
| } | ||
|
|
||
| if (!Array.isArray(data)) { | ||
| return res.status(400).send("Body must be an array"); | ||
| } | ||
|
|
||
| if (!data.every(item => typeof item === "string")) { | ||
| return res.status(400).send("Array must contain only strings"); | ||
| } | ||
|
|
||
| req.body = data; | ||
|
|
||
| next(); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export default function usernameMiddleware(req, res, next) { | ||
| const username = req.header("X-Username"); | ||
|
|
||
| req.username = username || null; | ||
|
|
||
| next(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to commit this file here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first commit is for middleware files and directory inside middleware and "adding files directories" is for off the shell files and folders inside that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. The middleware files look good to me. But can you remove the chat app files if these are not required for this specific task?