https://github.com/israel-laguan/express-production-template
Template for backends with NodeJS + Express, ready to use in production!
https://github.com/israel-laguan/express-production-template
api dotenv express-js middlewares nodejs pm2 rest winston
Last synced: 2 months ago
JSON representation
Template for backends with NodeJS + Express, ready to use in production!
- Host: GitHub
- URL: https://github.com/israel-laguan/express-production-template
- Owner: Israel-Laguan
- License: apache-2.0
- Created: 2020-04-07T06:25:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-19T13:39:11.000Z (over 5 years ago)
- Last Synced: 2025-03-29T16:34:09.181Z (7 months ago)
- Topics: api, dotenv, express-js, middlewares, nodejs, pm2, rest, winston
- Language: JavaScript
- Size: 17.6 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues-open][issues-open-shield]][issues-url]
[![Issues-closed][issues-closed-shield]][issues-url]
[![Contributors][contributors-shield]][contributors-url]
[![contributions welcome][contributions-welcome]][issues-url]
express-production-template
Template for backends with NodeJS + Express, ready to use in production!
🖊️
Read the article
🐞
Report a Bug
🙋♂️
Request Feature
## Table of Contents
1. [Author](#author)
2. [Contributing](#contributing)
3. [Show your support](#show-your-support)
4. [License](#license)
[](https://github.com/standard/standard)
# Features
[![Framework][badge-framework]][framework-url]
![javascript][]
![nodejs][]
![heroku][]
# Getting started
To get the Node server running locally:
- Clone this repo
- `npm install` to install all required dependencies
- Install MongoDB Community Edition ([instructions](https://docs.mongodb.com/manual/installation/#tutorials)) and run it by executing `mongod`
- `npm run dev` to start the local server
# Code Overview
## Dependencies
- [expressjs](https://github.com/expressjs/express) - The server for handling and routing HTTP requests
- [express-jwt](https://github.com/auth0/express-jwt) - Middleware for validating JWTs for authentication
- [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) - For generating JWTs used by authentication
- [mongoose](https://github.com/Automattic/mongoose) - For modeling and mapping MongoDB data to javascript
- [mongoose-unique-validator](https://github.com/blakehaswell/mongoose-unique-validator) - For handling unique validation errors in Mongoose. Mongoose only handles validation at the document level, so a unique index across a collection will throw an exception at the driver level. The `mongoose-unique-validator` plugin helps us by formatting the error like a normal mongoose `ValidationError`.
- [passport](https://github.com/jaredhanson/passport) - For handling user authentication
- [slug](https://github.com/dodo/node-slug) - For encoding titles into a URL-friendly format
## Application Structure
- `app.js` - The entry point to our application. This file defines our express server and connects it to MongoDB using mongoose. It also requires the routes and models we'll be using in the application.
- `config/` - This folder contains configuration for passport as well as a central location for configuration/environment variables.
- `routes/` - This folder contains the route definitions for our API.
- `models/` - This folder contains the schema definitions for our Mongoose models.
## Error Handling
In `routes/api/index.js`, we define a error-handling middleware for handling Mongoose's `ValidationError`. This middleware will respond with a 422 status code and format the response to have error messages the clients can understand
## Authentication
Requests are authenticated using the `Authorization` header with a valid JWT. We define two express middlewares in `routes/auth.js` that can be used to authenticate requests. The `required` middleware configures the `express-jwt` middleware using our application's secret and will return a 401 status code if the request cannot be authenticated. The payload of the JWT can then be accessed from `req.payload` in the endpoint. The `optional` middleware configures the `express-jwt` in the same way as `required`, but will *not* return a 401 status code if the request cannot be authenticated.
# Author
# Contributing
[![contributions welcome][contributions-welcome]][issues-url]
🤝 Contributions, issues and feature requests are welcome!
Feel free to check the [issues page][issues-url].
# Show your support
🤗 Give a ⭐️ if you like this project!
Icons from:
# License
[![License][badge-license]](http://badges.mit-license.org)
📝 This project is licensed under the [MIT](LICENSE)\
Feel free to fork this project and improve it
[contributors-shield]: https://img.shields.io/github/contributors/Israel-Laguan/express-production-template?style=for-the-badge
[contributors-url]: https://github.com/Israel-Laguan/express-production-template/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/Israel-Laguan/express-production-template?style=for-the-badge
[forks-url]: https://github.com/Israel-Laguan/express-production-template/network/members
[stars-shield]: https://img.shields.io/github/stars/Israel-Laguan/express-production-template?style=for-the-badge
[stars-url]: https://github.com/Israel-Laguan/express-production-template/stargazers
[issues-open-shield]: https://img.shields.io/github/issues/Israel-Laguan/express-production-template?style=for-the-badge
[issues-url]: https://github.com/Israel-Laguan/express-production-template/issues
[issues-closed-shield]: https://img.shields.io/github/issues-closed/Israel-Laguan/express-production-template?style=for-the-badge
[badge-framework]: https://img.shields.io/badge/express.js-v4.x-9cf?style=for-the-badge
[framework-url]: https://google.com
[contributions-welcome]: https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=for-the-badge
[badge-license]: https://img.shields.io/:license-mit-blue.svg?style=for-the-badge
[javascript]: https://img.shields.io/badge/JAVASCRIPT-ES6%2B-F7DF1E?style=for-the-badge&logo=javascript
[nodejs]: https://img.shields.io/badge/JAVASCRIPT-ES6%2B-F7DF1E?style=for-the-badge&logo=javascript