npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodejs-rest

v0.1.8

Published

Quick and easy restful nodejs api builder. Worry about your bussiness logics!

Downloads

30

Readme

nodejs-rest

Nodejs-rest is a quick and easy API builder framework, that lets you create powerful and professional API with most API features included including connection to multiple database connections.

  • The goal is to help you build your API by focussing more on your business logics, rather than tedious setups and redundant coding.

Installation

Installation is done using the npm install command:

$ npm install nodejs-rest

Features

  • Quick server configuration setup
  • Easy database connections.
  • API route setup with easy configurations
  • Route authentication
  • Hashing data to be stored on database or other persistance layer

Getting Start

The quickest way to get started with nodejs-rest is to start your node api project and install the nodejs-rest module

Creat your project:

  • *Make sure your project name is different from nodejs-rest
    // fill out the questionnaires
    $ npm init

    // for quick fill outs
    $ npm init -y

Install the package:

    $ npm install nodejs-rest --save

Create your index.js or server.js file in the root directory

  • A single file will sufice to build you API.
  • To build multiple apis with different database access point you can have multiple files (server1.js, server2.js,...) all created in the root directory.

Usage

The only import you are required to use the module is nodejs-rest.

const RestFactory = require("nodejs-rest");

const port = process.env.PORT || 3000;
const cors = ["https://localhost:3000"] // the default is *
const rest = RestFactory(port: number, cors?: string | string[]);

Database connection

rest.connect({
  port: 3306,
  database: "db_name",
  user: "user_name",
  password: "password",
  host: "host_address",
  database_type: "mysql",
});

Route configuration

You can set your routes prefixes, to give your different route projects senses. Like: https://mywebsite/api/v1/users/get/all

rest.landingRoute("/api/v1");

To add routes use the addRoute() function

Here is how route initialization and configuration structure looks like

rest.addRoute({
  method: "get" | "post" | "put" | "delete",
  url: string, // set your route url here `/user/get/all`
  action: {
    task:  "get" | "add" | "update" | "delete" , // tells the database what to do, in this case SELECT, UPDATE, INSERT, DELETE
    detail: {// detail has information about the retrieved data from connected database server
      table: string, // table name
      columns: string[], // array of columns from the respective database table, which are wanted for the SELECT query, default is *
      condition: string // sql conditions for query the WHERE query phrase,
    }
  },
  input_type: "params" | "query", // sets how you want your url to get parameters
  selectors: string[] // array of columns from the respective database table, where you are planning to pass as a where condition, which will be consumed from the url parameters
});

Examples

This route configuration generates the following api url https://mywebsite/user/get/all/:user_id

rest.addRoute({
  method: "get",
  url: "/user/get/all",
  action: {
    task: "get",
    detail: {
      table: "user",
      columns: ["user_first_name", "user_age", "user_status"],
      condition: "WHERE user_status=active LIMIT 100 ORDER ASC",
    },
  },
  input_type: "params", //(query, params) if input is expected to be passed in with GET request (optional)
  selectors: ["user_id"],
});

The same route configuration with input type query https://mywebsite/user/get/all/?user_id=1

{
  ...
  input_type: "query"
  ...
}

This is another route configuration example

rest.addRoute({
  method: "post",
  url: "/user/add",
  action: {
    task: "add",
    detail: {
      table: "user",
      columns: [
        "user_first_name",
        "user_last_name",
        "user_gender",
        "user_email",
        "user_password",
        "user_number",
      ],
    },
  },
});

The above route has the following features:

  • Accepts post request
  • Adds user's informations to user table
  • Operates INSERT database opertaion because of the following declaration
    {
        ...
        type: "add"
        ...
    }
  • And expects input arguments the same as columns array memebers
    {
        ...
        columns: [
        "user_first_name",
        "user_last_name",
        "user_gender",
        "user_email",
        "user_password",
        "user_number",]
        ...
    }

Build the routes' configurations

This command will inject all the routes' configurations listed above it, to the express router

rest.buildRoutes();

Start the server

This command starts your server

rest.init();

Todo

  • Route authorization and previlages
  • Middleware and modules injections

Contact

[email protected]

License

MIT