File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import express from "express" ;
2+ import usernameMiddleware from "./middleware/usernameMiddleware.js" ;
23
34const app = express ( ) ;
45
5-
66// built-in middleware
77app . use ( express . json ( ) ) ;
8+
9+ app . post ( "/" , usernameMiddleware , ( req , res ) => {
10+ const subjects = req . body ;
11+
12+ if ( ! Array . isArray ( subjects ) ) {
13+ return res . status ( 400 ) . send ( "Body must be a JSON array" ) ;
14+ }
15+
16+ if ( ! subjects . every ( item => typeof item === "string" ) ) {
17+ return res . status ( 400 ) . send ( "Array must contain only strings" ) ;
18+ }
19+
20+ const auth =
21+ req . username
22+ ? `You are authenticated as ${ req . username } .`
23+ : "You are not authenticated." ;
24+
25+ let message = "" ;
26+
27+ if ( subjects . length === 0 ) {
28+ message = "You have requested information about 0 subjects." ;
29+ } else if ( subjects . length === 1 ) {
30+ message = `You have requested information about 1 subject: ${ subjects [ 0 ] } .` ;
31+ } else {
32+ message = `You have requested information about ${ subjects . length } subjects: ${ subjects . join ( ", " ) } .` ;
33+ }
34+
35+ res . send ( `${ auth } \n\n${ message } ` ) ;
36+ } ) ;
37+
38+
39+ app . listen ( 4001 , ( ) => {
40+ console . log ( "Server running on 4001" ) ;
41+ } ) ;
You can’t perform that action at this time.
0 commit comments