api-server-library v1.0.2
API Server Library (for MongoDB)
A node package for building API servers with MongoDB super fast. Lightweight and simple to use, just enter a MongoDB username, password and database name and it's good to go.
How it works
- First add
const apiServer = require('api-server-library');
- Create a server object with
const server = new apiServer([MongoDB url], [Database name]);
- To access collections use
server.addCollection([Collection name]);
- To listen on specific paths use
server.get([path], [callback]);
- Then use
server.listen([port])
to start.
Common functions
new ApiServer([MongoDB url], [Database name]);
This is the constructor for creating the server.
Parameters
MongoDB url
A string in this format: mongodb://username:password@server-url.io:1234/
where each value is substituted for the correct version. The server-url.io
is the url at which the MongoDB server is located and the 1234
is the port number it is running on.
More details can be found here.
Database name
The name of the MongoDB database on the server
server.addCollection([Collection name]);
This function is used to tell the server that once server.listen()
is called this collection should be connected to and stored.
This can ONLY be called BEFORE server.listen()
. Once that has been called this function will no longer do anything.
Parameters
Collection name
The name of the collection
server.get([path], [callback]);
Tells the server NOT to send a 404 if the provided path is visited. Can be used both before and after server.listen()
but it is better to use of before.
Parameters
path
The path to listen on. Will look like /path1/
or /path2/
.
callback
The function which is called when the path is visited. Take 2 parameters, an APIReq
object and an APIRes
object.
This is where most of the server logic should be written. Use server.getCollection()
to get references to MongoDB collections then use standard MongoDB functions and methods to read and write to the database.
server.listen([port]);
Start the server.
Parameters
port
The port number to listen on.
server.getCollection([name]);
Returns a reference to a MongoDB collection whihch MUST already have been connected to via the server.addCollection()
. From this value you can then use standard MongoDB functions to read and write to the database.
Classes
APIReq
The first parameter given in the callback provided in server.get()
. Contains an object called parameters
. This is an object of keys and values where the keys are the parameter names given in the request and the values are their respective values.
For example:
// Request was: http://example.com/path1/?a=5
apiReq.parameters['a']; // Returns '5'
APIRes
The second parameter given in the callback provided in server.get()
. Contains two important methods.
setCode([code]);
Sets the response code for the HTTP response. For example, 404
or 200
. Must be called BEFORE .send()
.
send([object]);
Stringifies the given object, sends it and ends the response. Must be provided with a valid JSON object and be called AFTER .setCode()