baby-express

1.0.3 • Public • Published

babyExpress

A simple framework built on the node HTTP module that aims to simplify the creation and use of REST APIs.

Code Fellows 401 JS assignment by Kenneth Suh and Sabrina Tee

Quick Start

Install babyExpress from npm:

npm install baby-express

And require the babyExpress module into the top of your working file:

const babyExpress = require('baby-express');

Make sure to include a call at the bottom of your file to create and listen to a server at a specified port:

babyExpress.listen(3000, () => {
  console.log('Server up');
});

Then create REST calls following the sample code below:

A Sample babyExpress File

This file will give you a taste of what babyExpress does:

const babyExpress = require('baby-express');
 
babyExpress.get('/rest', (req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.gift(JSON.stringify({"msg": "get test"}));
});
 
babyExpress.post('/rest', (req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.gift(JSON.stringify({"msg": "post test"}));
});
 
babyExpress.patch('/rest', (req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.gift(JSON.stringify({"msg": "patch test"}));
});
 
babyExpress.put('/rest', (req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.gift(JSON.stringify({"msg": "put test"}));
});
 
babyExpress.delete('/rest', (req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.gift(JSON.stringify({"msg": "delete test"}));
});
 
babyExpress.listen(3000, () => {
  console.log('Server up');
});

Features

res.gift()

babyExpress combines the res.write() and res.end() http methods into one package defined as:

res.gift('hello world');

Data Handler

There is a data handler feature that collects all the data chunks and passes the completed string into a callback function:

babyExpress.post('/', (req, res) => {
  babyExpress.data(req, (data) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.gift(data);
  });
});

View Handler

There is a view handler feature that writes the data from the file to the response object:

// Example 1:
babyExpress.get('[uri]', (req, res) => {
  babyExpress.view('[pathname]', res);
});
 
// Example 2:
babyExpress.get('/filePath', (req, res) => {
  babyExpress.view(__dirname + '/../public/test.txt', res);
});
 

Content-Type Header

This feature is already built into baby-express.js but if you would like to use the Content-Type Header function that automatically detects the file's extension and inserts the completed Content-Type into the header, require the babyExpress module at the top of your file:

res.writeHead(200, babyExpress.contentHead(pathname));

Acceptable MIME Types:

MIME TYPE Content-Type
txt 'text/plain'
html 'text/html'
dvi 'text/x-dvi'
xc 'text/x-c'
css 'text/css'
jpeg 'image/jpeg'
png 'image/png'
bmp 'image/bmp'
json 'application/json'
pdf 'application/pdf'

Issues? Suggestions? Comments?

Submit an issue on Github or contact tyler@code-fellows.org

License

The MIT License (MIT)

Copyright (c) 2016 Kenneth Suh and Sabrina Tee

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.

Readme

Keywords

none

Package Sidebar

Install

npm i baby-express

Weekly Downloads

7

Version

1.0.3

License

MIT

Last publish

Collaborators

  • sabbyt