A quick and simple way to create a structured restAPI using, NodeJS, MongoDB and Mongoose.
Just add this code and it will automatically add any additional files you place in the api folder. the route for the api will be domain.com/apiPath/filename
where
- apiPath is the apiPath you set in the server file
- filename is the name of the file in the api folder
var neData = require('ne-data');
var dirName = __dirname;
var apiPath = "/api";
neData.server(server, dirName, apiPath);
Define the schema in any way you want and create as many as you want.
These files need to be in a folder matching the apiPath you provides in the same directory as the server file
var router = require('express').Router();
var mongoose = require('mongoose');
var neData = require('ne-data');
var pageSchema = {
path:{type: String, required: true},
title:{type: String, required: true},
description:{type: String, required: true},
pd:{
pdNumber:{type: Number, required: false, default: '0'},
pd1:{
path: {type: String, required: false}
}
},
createdAt:{type: String, required: true}
};
var Model = mongoose.model(
'Page',
pageSchema,
'page');
neData.model(router, Model);
module.exports = router;
Post The entire request body becomes the object that is posted
Get Get by Id Get with Limit results to number Get with Limit and cycle
Put Put by Id
Delete Delete by Id
Use query field value pairs find specific documents http://localhost:3001/api/page?f1=pathField&v1=pathFieldValue
- get will get the document where the pathField matches the pathFieldValue
- delete will delete the document where the pathField matches the pathFieldValue
- put will edit the document where the pathField matches the pathFieldValue, two addidional query params are needed to give the field you want to edit and the new value for the that field
If you are interested in more details on the working of the api please ask.
- Connect to mongoDB using mongoose
- Require and NPM install the dependencies
- Define your schema using mongoose syntax
- Use the schema to create a Model
- Activate neData by calling neData.init(router, Model)
post request
where
- people = name of the model/collection
- req.body = the document you want to add
- example req.body
{
"firstName": "John",
"lastName": "Jackson",
"email": "john@jackson.com"
}
results
- Adds the contents of the req.body as a document to the people collection using the mongoose schema.
comments
- Example post req with Json body using Postman.
get request
where
- api = the apiPath you specified
- people = name of the model/collection
results
- all documents in the people collection
get request
where
- api = the apiPath you specified
- people = name of the model/collection
- query: limit = the number of documents you want
results
- the first 3 documents in the people collection
get request 1
where
- api = the apiPath you specified
- people = name of the model/collection
- query: limit = the number number of documents in a cycle
- query: batch = the cycle number
results
- the 1st, 2nd and 3rd document in the collection
get request 2
where
- api = the apiPath you specified
- people = name of the model/collection
- query: limit = the number number of documents in a cycle
- query: batch = the cycle number
results
- the 4th, 5th and 6th document in the collection
get request 3
where
- api = the apiPath you specified
- people = name of the model/collection
- query: limit = the number number of documents in a cycle
- query: batch = the cycle number
results
- the 7th, 8th and 9th document in the collection
get request
where
- api = the apiPath you specified
- people = name of the model/collection
- query: f1 = the field you want to search by
- query: v1 = the value in the f1 filed you are looking to match
results
- all documents in the people collection where the firstName field is John
comments
- the search in case sensitive, so if you type john instead og Johh it will not find it.
get request
where
- api = the apiPath you specified
- people = name of the model/collection
- 55eefa231c0eba0d2c72af40 = the _id of the document you want
results
- The document with the _id of 55eefa231c0eba0d2c72af40
Search for documents where a field matches a value and give a new value to a field in those documents
put request
where
- api = the apiPath you specified
- people = name of the model/collection
- query: f1 = the field you want to search by
- query: v1 = the value in the f1 filed you are looking to match
- query: fs1 = the field you want to edit
- query: vs1 = the new value for the field you want to edit
results
- all documents in the people collection where the firstName field is John gets given a new value of john@jackstrade.com for the email field
comments
- the search in case sensitive, so if you type john instead og Johh it will not find it.
- you can search by one field and edit another field
- if more than one document matches the search then all those documents will be updated
- By default only one document is edited if you want to edit all documents matching the query then at a query param called multi=yes (?multi=yes or &multi=yes)
Search for documents where a field matches a value and use a json request body object to update the fields
put request
where
- api = the apiPath you specified
- people = name of the model/collection
- query: f1 = the field you want to search by
- query: v1 = the value in the f1 filed you are looking to match
results
- all documents in the people collection where the firstName field is John
comments
-
See the MongoDB docs on how to format the json req body (http://docs.mongodb.org/manual/reference/method/db.collection.update)
-
By default only one document is edited if you want to edit all documents matching the query then at a query param called multi=yes (?multi=yes or &multi=yes)
-
for
{
"_id": "55eefa231c0eba0d6c72af40",
"firstName": "John",
"lastName": "Jackson",
"email": "mail@mail.com",
"detail": {
"thing": "thing value"
},
"__v": 0,
"createdAt": "Fri Sep 18 2015 17:09:51 GMT+0200 (SAST)"
}
- use this reg body object to define what to edit
{
"detail.thing": "new thing value",
"email": "newmail@mail.com"
}
put request
where
- api = the apiPath you specified
- people = name of the model/collection
- 55eefa231c0eba0d6c72af40 = the _id of the document you want to edit
results
- all documents in the people collection where the firstName field is John
comments
-
See the MongoDB docs on how to format the json req body (http://docs.mongodb.org/manual/reference/method/db.collection.update)
-
By default only one document is edited if you want to edit all documents matching the query then at a query param called multi=yes (?multi=yes or &multi=yes)
-
for
{
"_id": "55eefa231c0eba0d6c72af40",
"firstName": "John",
"lastName": "Jackson",
"email": "mail@mail.com",
"detail": {
"thing": "thing value"
},
"__v": 0,
"createdAt": "Fri Sep 18 2015 17:09:51 GMT+0200 (SAST)"
}
- use this reg body object to define what to edit
{
"detail.thing": "new thing value",
"email": "newmail@mail.com"
}
delate request
where
- people = name of the model/collection
- api = the apiPath you specified
- query: f1 = the field you want to search by
- query: v1 = the value in the f1 filed you are looking to match
results
- remove documents in the people collection where the firstName field is John
comments
- the search in case sensitive, so if you type john instead og Johh it will not find it.
delete request
where
- people = name of the model/collection
- 55eefa231c0eba0d2c72af40 = the _id of the document you want to remove
results
- The document with the _id of 55eefa231c0eba0d2c72af40 is removed
Example dataRef object
var dataRef = {
"name": "emails",
"slug": "/admin/emails",
"apiSlug": "/data/emails",
"interfaceType": "default",
"cycleByDefault": false,
"batchSize": 10,
"type": "noEdit",
"categories": ["undefined"],
"tags": [],
"fields": [
{
name: "p1",
data: "nameFirst",
type: "noEdit"
},
{
name: "p2",
data: "nameLast",
type: "noEdit"
},
{
name: "p3",
data: "email",
type: "noEdit"
},
{
name: "p3",
data: "user",
type: "ObjectId"
}
]
};
This is used when displaying data in this field
displayType: "string"
- This field contains a string
- This is the default
- Not not need to add this it is assumed that a field is a string unless otherwise specified
displayType: "ObjectId"
- this field is the object id of another document in another collection
displayType: "array"
- this field is an array of strings
This is used when editing data in this field
editType: "text"
- This fieled contains a string
- This is the default
- Not not need to add this it is assumed that the field can be edited with a text input
editType: "noEdit"
- can not edit this field in ne-admin
editType: "select"
- Select the value of this field from a list of values
- if this edit type is specified the editOptions array must also be provided
- selectOptions: ["option1", "option2"]
editType: "textarea"
- The user is given a textarea instead of a text box to edit the field.
editType: "html" (Future feature)
- The user is given a html area instead of a text box to edit the field.
editType: "" (Future feature)
- The user is given a html area instead of a text box to edit the field.
The MIT License (MIT)
Copyright (c) 2015 Bernard Hamann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.