Skip to content

souravbapari1/mon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to the **Monpress**! This tool helps you create and manage Express-based Monpress projects with
✅ File-based routing
✅ Built-in REST method handlers
✅ Global middleware support
✅ Fast and flexible development experience

📦 Installation

Make sure you have Node.js installed.

Install Monpress globally via npm:

npm install -g monpress

🚀 Usage

The Monpress includes several commands to streamline your workflow:

🔧 Create a New Project

monpress create

You'll be prompted to:

  • Enter a project name
  • Choose a package manager (npm, yarn, or pnpm)

🧑‍💻 Start the Development Server

monpress dev

Launches your Monpress project in development mode with file-watching enabled.


🛠️ Generate Routes

monpress generate

Automatically generates route mappings from your file structure.


📁 File-Based Routing

Monpress uses the routes/ directory to define routes via filenames. This system is inspired by modern frameworks like Next.js and SvelteKit.

🧭 Route Mapping Example

File Path Route Path
routes/index.ts /
routes/about.ts /about
routes/contact.ts /contact
routes/blog.ts /blog
routes/blog/[blog].ts /blog/:blog
routes/user/[id_].ts /user/:id?
  • [] denotes dynamic segments
  • [_] (trailing underscore) denotes optional segments

🧩 Route File Example (routes/index.ts)

import { httpRequest } from "monpress";

export const GET = httpRequest(async (req, res) => {
  res.json({ message: "GET request successful" });
});

export const POST = httpRequest(async (req, res) => {
  res.json({ message: "POST request successful" });
});

export const PATCH = httpRequest(async (req, res) => {
  res.json({ message: "PATCH request successful" });
});

export const PUT = httpRequest(async (req, res) => {
  res.json({ message: "PUT request successful" });
});

export const DELETE = httpRequest(async (req, res) => {
  res.json({ message: "DELETE request successful" });
});

You can export any HTTP method handler (GET, POST, etc.) as needed.


🧱 Middleware

Monpress supports global middleware using the middleware() helper. Useful for:

  • Authentication
  • Logging
  • Custom headers
  • Request preprocessing

📌 Creating Middleware

import { middleware } from "monpress";

export const authMiddleware = middleware((req, res, next) => {
  if (req.path.startsWith("/auth")) {
    req.user = {
      id: "1",
      name: "Sourav",
    };
  }
  next();
});

🔗 Registering Middleware

Add middleware when initializing your Monpress app:

import { MonPress } from "monpress";
import { authMiddleware } from "./middlewares/auth";
import routes from "./routes";

const mon = MonPress({
  routes,
  middleware: [authMiddleware],
  express(app, http) {
    // Optional: custom Express logic here
  },
});

✅ All registered middleware is executed before your route handlers.


📁 File Uploading with Multer

Monpress supports file uploading using the popular multer middleware. Here's how you can integrate it into your Monpress application:

📦 Installation

First, install multer:

npm install multer

⚙️ Configuration

Create a file, for example, routes/upload.ts, to handle file uploads. Here's an example of how to configure multer and create a route:

import { httpRequest } from "monpress";
import multer from "multer";
import path from "path";

// Configure storage for uploaded files
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads/"); // Folder to store uploaded files
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
    cb(null, uniqueSuffix + path.extname(file.originalname)); // e.g., 1624567890-123456789.jpg
  },
});

// Create the multer upload instance
const upload = multer({ storage: storage });

// Example POST request handler with file upload
export const POST = httpRequest(async (req, res) => {
  upload.any()(req, res, async (err: any) => {
    if (err) {
      return res.status(500).json({ message: "Error uploading file" });
    }
    // Files are available in req.files, and other form data in req.body
    res.json({ files: req.files, body: req.body });
  });
});

📍 Route Definition

In this example, the POST route uses upload.any() to handle any number of files. You can also use upload.single('fieldName') to handle a single file or upload.array('fieldName', maxCount) to handle a specific number of files.

🧪 Testing the Upload

You can test this endpoint using curl:

curl -X POST -F "file=@/path/to/your/file.jpg" http://localhost:3000/upload

Make sure to replace /path/to/your/file.jpg with the actual path to your file.


🔄 Example Workflow

# Create a new project
monpress create

# Move into the project directory
cd my-project

# Start the dev server
monpress dev

# Generate route files
monpress generate

🛠 Commands

Command Description
monpress create Create a new Monpress project
monpress dev Start the development server
monpress generate Generate route mappings from files
monpress --help Show help and usage info

🤝 Contributing

Want to contribute? Here's how:

  1. Fork the repo
  2. Create a new branch
  3. Make your changes
  4. Submit a pull request

📄 License

Licensed under the MIT License.


Happy coding with Monpress! ⚡
File-based routing meets Express power.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published