首页 > > 详细

NodeJS辅导GET/POST/PUT/DELETE Backend调试Java、MongoDB编程辅导

REST Specification

You need to make sure your package.json file is configured properly so when we run the following set of commands, it sets up automatically.

mongod --dbpath=./data/ - this will start the mongodb server

In another console

npm install - this will install all the node packages you specified as dependencies.

npm start - this command will start your app. It will be your responsibility to make sure this command of this.

Your package.json will look something like

1
2
3
4
5
6
7
8
9
10
#123;
"name": "App Name here"
...
"scripts":#123;
"start": "Your start command here"
#125;
"dependencies":#123;
...
#125;
#125;

Lastly, make sure your app is bound to port 3000, so when we call localhost:3000, we can access the front end.

Users

The API should be able to perform CRUD operations on users (what kind of rating app would it be with no users?). The JSON specifications of a user is below, under POST /users.

  • GET /users - get all the users, ordered by username ascending, in an array under the key users.
  • GET /users?query - same as above and filtered (exact) by the possible query:
    • firstname
    • lastname
    • age
    • sex

An example would be /users?firstname=Tomamp;sex=M could return a JSON object containing a field users which is an array of User Objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#123;
"users": [
#123;
"_id": "4723",
"username": "gump1994",
"firstname":"Tom",
"lastname":"Hanks",
"sex": "M",
"age": 60
#125;,
#123;
"_id": "572",
"username": "h0rcrux",
"firstname":"Tom",
"lastname":"Riddle",
"sex": "M",
"age": 71
#125;,
#123;
"_id": "192",
"username": "m1ssionP0zzible",
"firstname":"Tom",
"lastname":"Cruise",
"sex": "M",
"age": 54
#125;
]
#125;

 

Individual Users

Next, we want to be able to get and modify users.

  • POST /user - in the body of the post request, supply all required fields and support any optional fields. See below on the schema required. If the username provided already exists or is not provided, return a 403 status. If the request is valid, return a 200 status with the new user returned.

NOTE: There are multiple ways to go about making a username unique. Your _id field therefore may be different from above but ensure your username field is always there!

1
2
3
4
5
6
7
8
#123;
"_id": #123;type:String#125;, //Will be different depending on your implementation, could be Number
"username": #123;type: String, required:true, unique:true#125;,
"firstname": #123;type: String, default:""#125;,
"lastname": #123;type: String, default:""#125;,
"sex": #123;type: String, default:""#125;,
"age": #123;type: Number, default: 0#125;
#125;

 

  • GET /user?id= - get a user by a specific ID. All objects therefore must have a _id field. If the ID given does not exist, return a 404 status.

An example would be /user?id=192 returns

1
2
3
4
5
6
7
8
#123;
"_id": "192",
"username": "m1ssionP0zzible",
"firstname":"Tom",
"lastname":"Cruise",
"sex": "M",
"age": 54
#125;

 

  • GET /user?username= - get a user by a specific username. If the username given does not exist, return a 404 status.

An example would be /user?username=m1ssionP0zzible returns

1
2
3
4
5
6
7
8
#123;
"_id": "192",
"username": "m1ssionP0zzible",
"firstname":"Tom",
"lastname":"Cruise",
"sex": "M",
"age": 54
#125;

 

  • DELETE /user?id= - deletes a user by a specific ID. Return 404 if the user does not exist. When deleting a user, also delete their reviews. (See below). e.g.
    /user?id=192 would remove the user with 192 as their id. Calling it again would result in a 404 response.

  • PUT /user?id= - updates an already existing user via the body. If the username key is passed as well, ignore the username key. If the user doesn’t exist, return a 404 error. If the request is valid, return a 200 with the updated user returned. We will assume all fields passed are fields in the user schema.

Example before:

1
2
3
4
5
6
7
8
#123;
"_id": "231",
"username": "TotallyNotAFakeUser",
"firstname":"Nigerian",
"lastname":"Prince",
"sex": "M",
"age": 174
#125;

 

PUT /user?id=231 with body:

1
2
3
4
5
6
#123;
"username":"shouldNotChange",
"firstname":"HongKong",
"lastname":"banker",
"age": 28
#125;

The database now looks like the following and should return:

1
2
3
4
5
6
7
8
#123;
"_id": "231",
"username": "TotallyNotAFakeUser",
"firstname":"HongKong",
"lastname":"banker",
"sex": "M",
"age": 28
#125;

Stores

  • GET /stores - gets all stores, ordered by storename in ascending order (In case of a tie, they should be sorted by ID in ascending order), as an array in the key stores
  • GET /stores?query Same as above and filtered (exact) by the query:
    • category
    • storename

e.g. /stores?category=department would return:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#123;
"stores": [
#123;
"_id": "4231",
"storename": "gartet",
"category":"department",
"Address":"123 Steals Avenue"
#125;,
#123;
"_id": "133",
"storename": "mallWart",
"category":"department",
"Address":"405 Blore Street"
#125;,
#123;
"_id": "431",
"storename": "mallWart",
"category":"department",
"Address":"83 Dawn Mills Road"
#125;,
#123;
"_id": "192",
"storename": "One Square",
"category":"department",
"Address":"831 Young Street"
#125;
]
#125;

Individual Stores

For stores, chains may share the same name. Therefore, their only identifier is their _id.

  • POST /store - in the body of the post request, supply all required fields and include any optional fields. See below on the schema required. Return a 200 if the request is valid with the newly created store. Return a 403 if no storename is provided or the storename is blank.

    1
    2
    3
    4
    5
    6
    #123;
    "_id": #123;type:String#125;,
    "storename": #123;type: String, required:true#125;,
    "department": #123;type: String, default:""#125;,
    "address": #123;type: String, default:""#125;
    #125;
  • GET /store?id= - get a store by a specific ID. All objects therefore must have a _id field. If the ID given does not exist, return a 404 status.
    An example would be /store?id=192 returns:

    1
    2
    3
    4
    5
    6
    #123;
    "_id": "192",
    "storename": "One Square",
    "category":"department",
    "Address":"831 Young Street"
    #125;
  • DELETE /store?id= - deletes a store by a specific ID. Return 200 status if the store exists. Return 404 if the store does not exist. When deleting a store, also delete their reviews. (See below).
    /store?id=192 would remove the store with 192 as their id. Calling it again would result a 404 response.

  • PUT /store?id= - updates an already existing store via the body. If the store doesn’t exist, return a 404 error. Assume all fields passed are fields in the store schema. Return a 200 if the request is valid with the updated store.
    Example before:

    1
    2
    3
    4
    5
    6
    #123;
    "_id": "192",
    "storename": "One Square",
    "category":"department",
    "Address":"831 Young Street"
    #125;
  • PUT /store?id=192 with body:

    1
    2
    3
    4
    #123;
    "storename": "One Square Budson's Hay",
    "category":"clothing"
    #125;

The database now looks like this and should return:

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!