0.0.9 • Published 17 days ago

merlee.js v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

npm npm npm npm

Welcome to merlee.js!

merlee.js is a backend nodejs rest api framework that lets you focus on the backend web fundamentals to deliver a simple and resilient apis that deploy to any Node.js server environments

Table of Contents

Prerequisites

  • Nodejs a free, open-sourced, cross-platform JavaScript run-time environment that lets developers write command line tools and server-side scripts outside of a browser.

Installation

Assuming your already have Nodejs installed on your machine. create a directory to hold your application. and change directory into the newly created directory

$ mkdir my-app
$ cd my-app

Use the npm init command to create a package.json file for your project

$ npm init

Now install merlee.js into the directory you just. For Example

$ npm install merlee.js

Hello world example

import merlee from 'merlee.js';
const app = merlee({
  port: 3000,
});

app.handler({ method: 'GET', path: '/' }, (req, res) => {
  res.send('Hello World');
});

app.listen((port) => console.log('listening on port:', port));

currently undefined routes juts hang, and do not send backend a response. The example above is a fully working server and when http://localhost:3000 is visited the server send backend a 200 response with Hello World

both thr req and res objects are the same objects that nodejs http provides but with a few modifications.

Basic Routing

The following examples illustrate how defining endpoints in merlee.js work

app is an instance of the Merlee Class

app.handler({ method: 'GET', path: '/' }, (req, res) => {
  // code goes here...
});

Real world projects often have different routes in different directories because of this merlee.js has an inbuilt routing system. to use the merlee.js router

  • create a new javascript file that has a function that returns an objects of objects that have router configuration. For example
// routes.js
function routes() {
  return {
    get: {
      path: '/users/friends',
      callback(req, res) {
        // do something
      },
    },
    post: {
      path: '/users/friends',
      callback(req, res) {
        // do something
      },
    },
  };
}

Middleware

The following examples illustrate how middleware can be used

const app = merlee({
  port: 3000,
  middleware: [checkAuth, checkSubscription],
});

app.handler({ method: 'GET', path: '/', middleware: [isAdmin] }, (req, res) => {
  // code goes here...
});

the method used on the endpoint is defined by the name of the key.

App Options

to pass app options use the set() method. the Merlee object has a set method which you could use to set global options for your apps. some inbuilt options port, static, views

optiondescription
portsets a port that the server runs on
viewssets a path to the ejs files directory
staticserves files in the directory as static

Below is an example of how to pass app options

const app = merlee({ port: 3000 });

app options can be set by passing them directly to the Merlee class. For example

const app = merlee({ port: 3000 });

Serving Static Files

To serve static files such as css, images or other types of files. set the app option static to the directory you would like to be served by the server. Below is an example of how to set the static method

const app = merlee({ static: './public' });

now all the files in the public directory are served as static files. now the files can be access:

http://localhost:3000/public/style.css
http://localhost:3000/public/app.js
http://localhost:3000/public/cat.png

Basic Example

import merlee from 'merlee.js';
const app = merlee({ port: 8080, views: 'src/views', static: 'public' });

//  get request
app.handler({ path: '/', method: 'get' }, (req, res) => {
  res.send('Hello World!');
});

app.handler({ path: '/home', method: 'get' }, async (req, res) => {
  const posts = await Posts.find({});

  // supports ejs out of the box;
  res.render('home', { posts });
});

// post request
app.handler({ path: '/', method: 'post' }, (req, res) => {
  res.send({ ...req.body }, 201);
});

app.listen((port) => console.log(`listening on port ${port}`));

would like to learn more about ejs ? click this link

Creating an app

to initialize a new app

const app = merlee({ port: 8080 });

the following methods can be passed to the app object |method|description | |------|------------------------------| |port | sets port for server to run on | |views | sets path to ejs views folder | |static | sets directory for static files |

giving the app options

const app = merlee({
  port: 8080,
  views: 'src/ejs',
  static: 'public',
});

handling requests

app.handler({ path: '/', method: 'get' }, (req, res) => {
  // req object - get data from client
  // res object - sends data to client
});
reqdescription
bodycontains form or json data
paramscontains url search parameters

handling responses

responses (send, sendFile, render) | res | description | | ---------- | ---------------------------------- | | send | responds with json data | | render | sends processed ejs file to client | | sendFile | sends file to client |

routing

creating a router

// # routes/room.js
module.exports = const router = () => {
  return {
    get:{
      path:'/room',
      callback(req, res){
        res.send('hello from the router')
      }
    },
    post:{
      path:'/room',
      callback(req, res){
        res.send(req.body)
      }
    }
  }
}

// # server.js
import Merlee from 'merlee.js'
import {router} from './routes/room'
const app = new Merlee();


app.handler(router);

Contributing

feel free to contribute and make merlee.js a better package.

File Structure

filedescription
bodyhandles request body
statichandles static files
fileTypeshandles content types for static files
merleemain file, connectes all above together

License

MIT License

License

this project is MIT Licensed

Author

0.0.9

17 days ago

0.0.8

1 year ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago