1.0.1 • Published 8 years ago
json_node_server v1.0.1
json_node_server
json_node_server is a lightweight nodeJS library for the creation of simple APIs. It supports the methods GET, POST, PUT and DELETE and handles JSON payloads by default.
Installation
npm install @lomsansnom/json_node_server
Usage
const server = require('@lomsansnom/json_node_server')(name, options);
Options
port- Listening port numberverbose- Enable logging (for debugging purpose), defaultfalsejson-trueto enable json middleware (set the response content-type to application/json), defaultfalsecors-trueor{ allowOrigin: ''}to enable cors middleware, defaultfalse
Register routes
/*
* path - String, with or without leading '/'. Parameters can be defined with the syntax :paramName
* callback - Function
* params - (req, res) Node request and response objects.
*/
server.get(path, callback);
server.post(path, callback);
server.put(path, callback);
server.delete(path, callback);Parameters
query string
Query string is converted to an Object and stored under
req.query// GET /sample?id=1&name=sample server.get('/sample', (req, res) => { console.log(req.query) // Outputs {id: '1', name: 'sample'} });URL parameters
URL parameters are defined in the path with the syntax
:parameterName. They are converted to an Object and stored under req.params// GET /sample/1/sample server.get('/sample/:id/:name', (req, res) => { console.log(req.params) // Outputs {id: '1', name: 'sample'} });body
body must be a valid JSON string and is automatically parsed and stored under
req.body// POST /sample, {id: 1, name: 'sample'} server.post('/sample', (req, res) => { console.log(req.params) // Outputs {id: 1, name: 'sample'} });
Middlewares
jsonmiddleware addsContent-Type: application/jsonto response headerscorsmiddleware handlesOPTIONSrequests in order to allow cross origin requests. Adds the following to reponse headers:{ Access-Control-Allow-Origin: options.allowOrigin, Access-Control-Allow-Methods: 'GET, POST, PUT, DELETE' }- Custom middlewares can be added by using the function
server.use(middleware), where middleware must be a function taking(req, res)as parameters or an object with a functiondo(req, res)