Demo for Mongoose Slugger using Express.
This service allows adding “users” to the database and creates unique slugs in regard to the city
property.
-
Install dependencies with Yarn
$ yarn
-
Make sure a MongoDB is running at
mongodb://localhost:27017
. Use e.g. Docker:$ docker run -p 27017:27017 --name slugger-demo -d --rm mongo:3.4.6
-
Start the server
$ yarn start
The following routes are defined:
GET /users
-- get all usersGET /users/:city
-- get all users within a cityGET /users/:city/:slug
-- get one specific userPOST /users
-- add a new user byPOST
ing JSON data
-
Create 'John Doe' in Denver. For an empty database, he’ll get the slug
john-doe
:$ curl -H "Content-Type: application/json" -X POST -d '{ "firstname": "john", "lastname": "doe", "city": "denver" }' http://localhost:3000/users {"_id":"5adb08ffaac24808f18fa057","firstname":"john","lastname":"doe","city":"denver","slug":"john-doe","__v":0}
-
Create another 'John Doe' in Denver. He’ll get the slug
john-doe-2
:$ curl -H "Content-Type: application/json" -X POST -d '{ "firstname": "john", "lastname": "doe", "city": "denver" }' http://localhost:3000/users {"_id":"5adb090faac24808f18fa058","firstname":"john","lastname":"doe","city":"denver","slug":"john-doe-2","__v":0}
-
Create 'John Doe' in Memphis. He’ll get slug
john-doe
, because there’s no other John Doe in Memphis yet:$ curl -H "Content-Type: application/json" -X POST -d '{ "firstname": "john", "lastname": "doe", "city": "memphis" }' http://localhost:3000/users {"_id":"5adb0926aac24808f18fa059","firstname":"john","lastname":"doe","city":"memphis","slug":"john-doe","__v":0}
-
Get a specific entry by city and slug (e.g.
john-doe-2
in Denver):$ curl http://localhost:3000/users/denver/john-doe-2 {"_id":"5adb090faac24808f18fa058","firstname":"john","lastname":"doe","city":"denver","slug":"john-doe-2","__v":0}
Copyright Philipp Katz, LineUpr GmbH, 2018