This is a simple key-based auth server written in Node.js. The modules used in this project are:
- body-parser - Parse all request bodies
- console-stamp - All console logs have data like timestamps
- express - Create HTTP server
- mongo-sanitize - Sanitize inputs
- mongoose - ORM for interacting with MongoDB
- morgan - Log server events
- randexp - Key generation
- require-all - Dynamic route creation
- uuid - Use in place of MongoDB ObjectId
-
View key schema here
-
Admin Endpoints
- POST /key/new - Creates new key and returns its ID
- Body: None
- 201 Created returned if operation successful
- GET /key/:id - Gets key by ID and shows relevant information
- PUT /key/:id - Updates key with machineId in request body
- Body:
{ "machineId": "something" } - 200 OK returned if operation successful
- Body:
- POST /key/new - Creates new key and returns its ID
-
Other Endpoints
- POST /key/verify - Verifies if machineId and key in body are in database
- Body:
{ "key": "something", "machineId": "something" } - 200 OK returned if operation successful
- Body:
- POST /key/active - Checks whether key is currently bound to a specific device
- Body:
{ "key": "something" } - 200 OK returned if operation successful
- Body:
- POST /key/verify - Verifies if machineId and key in body are in database
Note: All responses have a 'message' attribute which gives a more detailed reason why a request failed, if it did, rather than just the status code.
To add routes, there are only a few simple steps:
- Create a new file in the
routes/folder. This will become the base of the route.- Creating a file called
example.jscreates a base route of/example
- Creating a file called
- Add this code to begin with:
'use strict';
module.exports = (router) => {
return router;
};Do NOT remove the module.exports or anything already inside it.
- To add routes, simply start coding like you would in a regular Express.js app!
'use strict';
module.exports = (router) => {
// GET /example/
router.get('/', (req, res) => res.send('hello there'));
// GET /test
router.get('/test', (req, res) => res.json({ message: 'this is a test' }));
return router;
};