| Welcome to the **Monpress**! This tool helps you create and manage Express-based Monpress projects with | ||||
|---|---|---|---|---|
|
Make sure you have Node.js installed.
Install Monpress globally via npm:
npm install -g monpressThe Monpress includes several commands to streamline your workflow:
monpress createYou'll be prompted to:
- Enter a project name
- Choose a package manager (
npm,yarn, orpnpm)
monpress devLaunches your Monpress project in development mode with file-watching enabled.
monpress generateAutomatically generates route mappings from your file structure.
Monpress uses the routes/ directory to define routes via filenames. This system is inspired by modern frameworks like Next.js and SvelteKit.
| 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
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.
Monpress supports global middleware using the middleware() helper. Useful for:
- Authentication
- Logging
- Custom headers
- Request preprocessing
import { middleware } from "monpress";
export const authMiddleware = middleware((req, res, next) => {
if (req.path.startsWith("/auth")) {
req.user = {
id: "1",
name: "Sourav",
};
}
next();
});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.
Monpress supports file uploading using the popular multer middleware. Here's how you can integrate it into your Monpress application:
First, install multer:
npm install multerCreate 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 });
});
});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.
You can test this endpoint using curl:
curl -X POST -F "file=@/path/to/your/file.jpg" http://localhost:3000/uploadMake sure to replace /path/to/your/file.jpg with the actual path to your file.
# 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| 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 |
Want to contribute? Here's how:
- Fork the repo
- Create a new branch
- Make your changes
- Submit a pull request
Licensed under the MIT License.
Happy coding with Monpress! ⚡
File-based routing meets Express power.