This is an Express starter template that includes custom error handling, error throwing, logging, and cookie parsing functionality.
- Custom Error Handling Middleware
- Error Thrower Utility
- Logger Middleware
- Cookie Parsing Middleware
-
Clone the repository:
git clone https://github.com/your/repository.git .
-
Install dependencies:
npm install
-
Start the server:
For Development:
npm run dev
For Production:
npm start
The custom error handling functionality is defined in the errors
folder. errorThrower.ts
returns a new error with the specified statusCode and message.
The errorThrower.ts
utility provides a convenient way to throw errors with custom status codes and messages.
Example usage:
import errorThrower from './errors/errorThrower';
// Inside a route handler
if (!user) {
throw errorThrower(404, 'User not found');
}
The logger middleware (winston.config.ts
) logs incoming requests with relevant information such as request method, URL, status code, and response time.
Use logger.info("<your message string>")
for logging regular messages. This will create a log in the logs/all.log
file.
For logging errors, use logger.error("<your error string>")
. This will create a log in the logs/error.log
file.
The cookie parsing middleware (cookieParser()
) parses incoming cookies from the request headers and makes them available in req.cookies
.
You can customize the error messages, logging format, and cookie settings by modifying the respective middleware files.
Contributions are welcome! Feel free to submit issues or pull requests to improve this starter template.