A template for starting full stack projects with express
. Includes
authentication, example resource and common middlewares.
- Download this template as a .zip file. DO NOT FORK AND CLONE
- Move the .zip file to your
sei/projects/
directory and Unzip it (creating a folder) -- NOTE: if the folder was already unzipped, use themv
command line to move it to thesei/projects/
directory. - Rename the directory from mongoose-express-liquid-boilerplate -> your-app-name.
- Find and Replace any mention of
mongoose-express-liquid-boilerplate
especially the one inpackage.json
- Empty
README.md
and fill with your own content. - Move into the new project and
git init
if not already a repository. - Run
touch .env
in the main project directory(same level asserver.js
) - Add
node_modules
and.env
to your.gitignore
if it's not already there(it should be) - Replace all example models, controllers, and views with your app's resources. Auth is provided for you already.
- Install dependencies with
npm install
. - Ensure that you have
nodemon
installed by runningnpm install -g nodemon
. - In your
.env
add variables forPORT
&DATABASE_URL
&SECRET
. This app uses mongoDb and Session authentication, so yourDATABASE_URL
will need to be your local mongo(see the full CRUD app's.env
for reference) - Ensure the API is functioning properly by running
npm start
. - Once everything is working, make an initial commit.
- Add features on other branches and merge them into the main branch as you complete them!
Dependencies are stored in package.json
.
The most important file for understanding the structure of the template is
server.js
. This is where the actual Express app
object is created, where
the routes are registered, and more. To register a routefile,
follow the pattern established here with exampleRoutes
and userRoutes
. If
you want to add any middlewares to your app, add it to the file utils/middleware.js
in the main export function, which is called in server.js
.
The main directory contains models, views, and controller files. Models are simply Mongoose
models. To create your own, follow the patterns established in
models/example.js
. Similarly, the controllers and views listed under the examples directories are a great starting point for your own resources.
The models/connection.js
file holds all mongoose connection setup steps and can be required and destructured from just like this (Schema and model given as examples):
const { Schema, model } = require('./connection.js')
You probably will only need to interact with files in /models
, /controllers
, /views
and server.js
.
This template uses npm
as a task runner.
These are the commands available:
Command | Effect |
---|---|
npm start |
Starts a development server with nodemon that automatically refreshes when you change something. |
Use this as the basis for your own documentation. Add a new third-level heading for your custom entities, and follow the pattern provided for the built-in user authentication documentation.
Verb | URI Pattern | Controller#Action |
---|---|---|
POST | /auth/signup |
users#signup |
POST | /auth/login |
users#login |
DELETE | /auth/logout/ |
users#logout |
Errors are handled with a default view, and should be called as seen in this example:
router.delete('/:id', (req, res) => {
const exampleId = req.params.id
Example.findByIdAndRemove(exampleId)
.then(example => {
res.redirect('/examples')
})
.catch(error => {
res.redirect(`/error?error=${error}`)
})
})
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].