1.0.23 • Published 5 months ago
exhandlers v1.0.23
Express Handlers
Utility Middlewares for working with Express.js
A simple middleware package for Express applications.
Documentation
Handlers
- Async Handler
- CORS Handler
- Error Handler
- Log Handler
- Not Found Handler
- Hash Handler
- Password Handler
- MongoDB Handler
- Postgres Handler
- Redis Handler
Installation
You can install exhandlers using npm:
npm install exhandlers
Usage
Contents of your entry point file would look like this
const express = require("express");
const {
asyncHandler,
errorHandler,
logHandler,
notFoundHandler,
corsHandler,
passwordHandler,
hashHandler,
mongoHandler,
initPostgres,
initRedis,
transports,
initLogger,
streamHandler,
} = require("exhandlers");
const User = require("path/to/userModel");
const app = express();
// Use CORS handler
app.use(corsHandler("http://localhost:3000, http://127.0.0.1:4321"));
const logger = initLogger({ transports });
// Use logging middleware
app.use(logHandler("combined", { stream: streamHandler(logger) }));
// Define routes with asyncHandler
app.get(
"/api/data",
asyncHandler(async (req, res) => {
const data = await fetchData(); // Replace with your async logic
res.json(data);
}),
);
// Use hashHandler to hash password
app.post(
"/api/auth/register",
asyncHandler(async (req, res) => {
const { password } = req.body;
const hashedPassword = await hashHandler(password);
}),
);
// Use passwordHandler to compare passwords
app.post(
"/api/auth/login",
asyncHandler(async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
const hashedPassword = user.password;
const passwordMatch = await passwordHandler(password, hashedPassword);
if (!passwordMatch) {
throw new Error("Invalid credentials");
}
}),
);
// Use Not Found handler for undefined routes
app.use(notFoundHandler);
// Use error handler at the end of the middleware stack
app.use(errorHandler);
const PORT = 3000;
// Use mongoHandler to connect to mongodb database
mongoHandler(
"mongodb://<username>:<password>@<host>:<port>/<database>?options",
);
// or
// Use initPostgres to connect to postgres database and postgresHandler to make queries
const pool = initPostgres({
connectionString: "postgres://<user>:<password>@<host>:<port>/<database>",
});
// Use initRedis to connect with redis
const client = initRedis({ url: "redis://<user>:<password>@<host>:<port>" });
app.listen(PORT, async () => {
console.log(`Server running @ port ${PORT}`);
});
License
This project is licensed under the MIT License.
1.0.23
5 months ago
1.0.22
6 months ago
1.0.21
6 months ago
1.0.20
6 months ago
1.0.19
6 months ago
1.0.18
7 months ago
1.0.17
7 months ago
1.0.16
7 months ago
1.0.11
7 months ago
1.0.10
7 months ago
1.0.15
7 months ago
1.0.14
7 months ago
1.0.13
7 months ago
1.0.12
7 months ago
1.0.9
8 months ago
1.0.8
8 months ago
1.0.2
8 months ago
1.0.1
8 months ago
1.0.7
8 months ago
1.0.6
8 months ago
1.0.5
8 months ago
1.0.4
8 months ago
1.0.3
8 months ago
1.0.0
9 months ago