Skip to content

Latest commit

 

History

History
62 lines (47 loc) · 1.28 KB

readme.md

File metadata and controls

62 lines (47 loc) · 1.28 KB

minimal-web-lib

A very minimal web library. This library is created for hammercode nodejs class learning materials.

Installing

npm install minimal-web-lib

API

Creating new instance

const Router = require('minimal-web-lib');

const router  = new Router();

Defining Routes

// Defining GET Route
router.get('/hello', (request, response) => {
  // request and response callback params are
  // node http-server request, response objects
  response.end('Helloo')
});

// Defining GET Route
router.get('/posts/:id/comments', (request, response) => {
  // Getting url param data { id: <string> }
  console.log(request.params)

  response.setHeader('content-type', 'application/json')
  response.end(JSON.strigify({
    message: 'Hey there'
  }))
});

// Defining POST Route
router.post('/posts/:id/comments:/:commentId', (request, response) => {
  // Getting body request
  console.log(request.data)
  // Getting url param data { id: <string>, commentId: <string> }
  console.log(request.params)

  response.setHeader('content-type', 'application/json')
  response.end(JSON.strigify({
    message: 'Hey there'
  }))
});

// Defining other routes
router.put('/a/path', callback);
router.patch('/a/path', callback);
router.delete('/a/path', callback);

Lisence

MIT