Coder Social home page Coder Social logo

express-routes-handler's Introduction

Express Routes Handler

Simple Express Routes Handler for Beginners

Now comes pre-installed with express


Simple start index.js

const express = require('express'); //ah yes, semicolons
/* Or you can do
const {express} = require('express-routes-handler');
*/
const handler = require('express-routes-handler');
const app = express();

Now create a folder with your desired name. I chose routes.
This folder will have all the routes folder.

Now create any file in there.
Example: status.js

Now put inside this code:

module.exports = {
  name: '/status', // This can be an array.
  disabled: false, // Can be omitted. Stops from the endpoint being loaded. Default is false
  method: 'get', // Can be omitted. Can be an array. Default is 'get'
  run: (req, res) => { // Like a normal function
    res.sendStatus(200)
  }
}
// Disabled endpoints show up at the top of the table in console

After you've done this go to the main index.js file and do this

const express = require('express'); //ah yes, semicolons
const app = express();
const handler = require('express-routes-handler');

handler(app,'./routes', /* Omittable => */(req,res) => {
  // res.status(404).send({status:404,message:"Not Found"})
  // or any code
}); // This loads all the endpoints in routes folder


//Now for the listener part
app.listen(/*Port name*/,/*Omittable callback function*/);

Now you've got yourself a simple express app.


Or to keep track of the requests

handler.keeptrack(app,/* Omittable too => */(data) => {
    console.log(data)
},/*Here lies the omitttable config*/)
// This must be put before the handler(app,'./routes') function

What may the config contain. (Already filled keys are default values)

const options = { 
    ip: true, // IP tracking enabled?
    ip_header: '', // IP tracking header
    agent: true, // User-agent tracking enabled?
    agent_header: 'user-agent', // User-agent header
    query: true, // Query parameter tracking enabled?
    route: true, // Route tracking enabled?
    method: true // Method tracking enabled? (new)
} 

Changelog 21-Dec-22

+ The page404 function is now built into the initialization function to avoid node asyncronous function execution problems

Upcoming changes

Read ability of multiple module exports in one file

This package may or may not be used in advanced development

This package will not be responsible for breaking of your express server (honestly, how that would even happen?)
Feel free to check the source code and suggest new features
More features will be added soon.

Made with ๐Ÿ’– by Server Developer#9447

Support Server
Developer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.