This is an easy, basic and raw example of HOW to implement an API with Node, Express and MongoDB/Atlas (with Mongoose ODM).
npm install
npm run dev
npm run build
npm start
40 records
.curl http://127.0.0.1:3333/api/users
{
"data": [
{
"_id": "60da6b74fc13ae7069000d4b",
"firstname": "Dedra",
"lastname": "Demangel",
"age": 96,
"gender": "Agender",
"username": "ddemangel3",
"company": "Skidoo",
"email": "ddemangel3@networkadvertising.org",
"phone": "759-142-2883",
"address": "4 Fisk Drive"
},
{
"_id": "60da6b74fc13ae7069000d48",
"firstname": "Martynne",
"lastname": "Pimmocke",
"age": 79,
"gender": "Female",
"username": "mpimmocke0",
"company": "Edgeify",
"email": "mpimmocke0@amazonaws.com",
"phone": "354-508-5487",
"address": "20633 Stone Corner Trail"
}
]
}
n
record(s) where n
is the value (type: Number) of the limit
key.curl http://127.0.0.1:3333/api/users?limit=1
{
"data": [
{
"_id": "60da6b74fc13ae7069000d4b",
"firstname": "Dedra",
"lastname": "Demangel",
"age": 96,
"gender": "Agender",
"username": "ddemangel3",
"company": "Skidoo",
"email": "ddemangel3@networkadvertising.org",
"phone": "759-142-2883",
"address": "4 Fisk Drive"
}
]
}
Wrong type for n
value will return all the users.
Example: users?limit=%27Hello%27
n
(PRIMARY KEY) where n
is the value (type: Number) of the offset
key.curl http://127.0.0.1:3333/api/users?offset=10
{
"data": [
{
"_id": "60da6b74fc13ae7069000d55",
"firstname": "Cindi",
"lastname": "Harnell",
"age": 57,
"gender": "Bigender",
"username": "charnelld",
"company": "Skyble",
"email": "charnelld@networkadvertising.org",
"phone": "176-961-8815",
"address": "09953 Susan Alley"
},
{
"_id": "60da6b74fc13ae7069000d57",
"firstname": "Olva",
"lastname": "Hoodless",
"age": 61,
"gender": "Female",
"username": "ohoodlessf",
"company": "Jaloo",
"email": "ohoodlessf@illinois.edu",
"phone": "886-598-0586",
"address": "8 Chinook Pass"
}
]
}
curl http://127.0.0.1:3333/latency
{
"data": "Thanks for waiting 1 second"
}
n
milliseconds where, min:1000 and max:4000. Default value: 1000ms.Wrong type for n
value will produce a default delay of 1000ms.
curl http://127.0.0.1:3333/latency?delay=2000
{
"data": "Thanks for waiting 2 seconds"
}
curl http://127.0.0.1:3333/
{
"message": "Node.js, Express, and MongoDB API!"
}
If you need help at the tie of Migrating from mLab to MongoDB Atlas