buddyexpress
v1.0.1
Published
A minimal http router framework close to express that can get the job done in most small apps.
Downloads
1
Maintainers
Readme
Project Description
Introduction
Buddy-Express is my own unique take on the popular Express framework with my own implementation.
Requirements
Here are the key features that our framework will provide:
Installation: You should be able to install our framework using the command
npm install buddyexpress
.Application Creation: You should be able to create an
buddyexpress
app using the following code:const app = buddyexpress()
.Middlewares: Our framework allows you to add middlewares using the
app.use((req, res, next) => {})
orapp.use('/<route>', (req, res, next) => {})
.Routes: We support the definition of
get
,post
,put
,delete
,patch
andall
routes.TypeScript Support:
buddyexpress
is strongly typed using TypeScript.Response Methods: The framework includes the response methods
res.status()
,res.send()
,res.json()
, andres.redirect()
.URL Query Parameters: You can access URL query parameters using
req.query
.URL Route Parameters: You can define routes with parameters. Example: define
/test/:id
and access the:id
fromreq.params
.Static Files:
buddyexpress
includes a built-in middleware to serve static files usingapp.use(app.static('public'))
.JSON Body Parser:
buddyexpress
includes a built-in json body parser middlewareapp.json()
using likeapp.use(app.json())
.
Usage
import { buddyexpress, Request, Response, NextFunction } from 'buddyexpress';
import path from 'path';
const requestLogger = (req: Request, res: Response, next: NextFunction): void => {
console.log(`${req.method} ${req.url}`);
next();
};
const additionalMiddleware = (req: Request, res: Response, next: NextFunction): void => {
console.log('Additional middleware executed');
next();
};
const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction
): void => {
console.error('Error:', err.message);
res.status(500);
res.json({ message: `Error: ${err.message}` });
};
const app = buddyexpress();
app.use(requestLogger);
app.use(app.json());
app.use('/files', app.static(path.join(__dirname, 'public')));
app.get('/', (req: Request, res: Response): void => {
res.status(200);
res.send('Hello, world!');
});
app.get('/user', additionalMiddleware, (req: Request, res: Response): void => {
const { name } = req.query;
res.status(200);
res.json({ name: name });
});
app.get(
'/test/:id/lol/:newID',
additionalMiddleware,
(req: Request, res: Response): void => {
const { id, newID } = req.params;
res.status(200);
res.send(`test number: ${id}, newID: ${newID}`);
}
);
app.post('/data', (req: Request, res: Response): void => {
res.status(200);
res.json(req.body);
});
app.delete('/data', (req: Request, res: Response): void => {
res.status(200);
res.send(`Received delete method on /data`);
});
app.get('/redirect', (req: Request, res: Response): void => {
res.redirect('https://www.example.com');
});
app.all('*', (req: Request, res: Response, next: NextFunction): void => {
next(new Error('Route not found'));
});
app.use(errorHandler);
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Tech Stack
Our framework is built using the following technologies:
- NodeJS
- TypeScript