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

morpress.js

v3.0.3

Published

A simple API framework without dependencies

Downloads

272

Readme


Morpress.js

Morpress.js is a lightweight and straightforward API framework for Node.js. It provides core functionalities for creating HTTP servers with custom routing, static file serving, rate limiting, and basic security measures, all without relying on Express.

Features

  • Routing: Define routes for HTTP methods (GET, POST, PUT, DELETE, PATCH)
  • Static File Serving: Serve static files from specified directories
  • Rate Limiting: Prevent abuse by limiting requests from a single IP
  • Security: Basic security enhancements with Helmet
  • Middleware Support: Use global middleware for request processing
  • Error Handling: Custom error handling

Installation

Install the necessary dependencies using npm:

npm install morpress.js

Usage

Basic Setup

Here’s a basic example to get started:

const { Morpress, Router } = require('morpress.js');

// Create an instance of Morpress.js
const app = new Morpress();

// Configure rate limiting
app.rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // Maximum 100 requests
});

// Add middleware
app.use((req, res, next) => {
    console.log(`Received request: ${req.method} ${req.url}`);
    next();
});

// Define routes
app.get('/hello', (req, res) => {
    res.json({ message: 'Hello, world!' });
});

app.post('/data', (req, res) => {
    res.json({ received: req.body });
});

// Serve static files
app.static('./public');

// Use a Router
const router = new Router();
router.get('/nested', (req, res) => {
    res.json({ message: 'Nested route' });
});
app.use('/api', router);

// Set error handler
app.setErrorHandler((err, req, res) => {
    res.status(500).json({ error: 'Something went wrong!' });
});

// Start the server
app.listen(3000, () => {
    console.log('Server running on port 3000');
});

Rate Limiting

Rate limiting helps control the number of requests an IP address can make in a given timeframe. Configure it with:

app.rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // Maximum 100 requests per window
});

Static File Serving

To serve static files, specify directories using the static method:

app.static('./public');

Error Handling

Define a custom error handler to manage errors:

app.setErrorHandler((err, req, res) => {
    res.status(500).json({ error: 'Something went wrong!' });
});

API

Morpress Class

  • use(path, middlewareOrRouter): Add global middleware or Router.
  • get(path, handler): Define a route for GET requests.
  • post(path, handler): Define a route for POST requests.
  • put(path, handler): Define a route for PUT requests.
  • delete(path, handler): Define a route for DELETE requests.
  • patch(path, handler): Define a route for PATCH requests.
  • static(path): Serve static files from the specified directories.
  • trustProxy(value): Configure proxy settings.
  • sessionOptions(options): Configure session settings.
  • rateLimit(options): Set rate limiting options.
  • setErrorHandler(handler): Define a custom error handler.
  • listen(port, callback): Start the server on the specified port.

Router Class

  • use(path, middleware): Add middleware to the Router.
  • get(path, handler): Define a route for GET requests.
  • post(path, handler): Define a route for POST requests.
  • put(path, handler): Define a route for PUT requests.
  • delete(path, handler): Define a route for DELETE requests.
  • patch(path, handler): Define a route for PATCH requests.
  • handle(req, res): Handle incoming requests.

License

This project is licensed under the MIT License. See the LICENSE file for more details.