Quickly Setup an Express Server
In only 3 steps!
- RESTful express server with less cruft than any 'generator'.
- Includes optimized defaults: CORS, logging & error handling!
- Video: 'Live coding' getting setup - ~2 min.
- Cheat: tl;dr automated shell script
- Instructions: 3 manual steps
2 shell scripts for the impatient: an express
~/express-setup.sh. And a route generator, with multiple choice and naming
RECOMMENDED: Create an empty folder for your express server &
cd <server-path>into it before running the script.
# Download script to create an app.js curl -o- -L https://raw.githubusercontent.com/justsml/guides/master/express/setup-guide/scripts/express-setup.sh > ~/express-setup.sh # Download script to add a route (will prompt user for route name & template type) curl -o- -L https://raw.githubusercontent.com/justsml/guides/master/express/setup-guide/scripts/express-create-route.sh > ~/express-create-route.sh # Set permission chmod a+x ~/express-*.sh
You're now setup with 2 local scripts. Each time you want to create an app, run the following commands:
# create a director, cd into it mkdir ~/my-app cd ~/my-app # Create the app.js (once) ~/express-setup.sh # Add a route (as many times as needed) ~/express-create-route.sh
🎉 Jump to # 3d to complete adding route(s) in
1. Install NPM Packages
# In folder w/ package.json: npm install express cors body-parser morgan nodemon
app.js into your project
app.js file and copy it to your project.
3a-d. Create Express Router Module(s)
This is the preferred way to implement paths like
3a. Create a folder
3b. Create a JS file for each route (path) in your API.
For example, if your URLs will include
/api/cat/, create 2 router files:
Copy & Paste a router template into your router
3c. Copy router template (by database/storage tech):
Template will contain a placeholder variable
items - search & replace using the route (table or model) name.
So the variable names should become
cat. (Note: singular name used - plural names will be included in replace)
Check for template- or database-specific TODO comments in route files.
3d. Enable Routes in
Add your routes to
app.js with code like:
app.use('/api/cat', require('./routes/cat')) app.use('/api/dog', require('./routes/dog'))
Or with ES6 Modules:
import cat from './routes/cat' import dog from './routes/dog' // custom routes go after cors, logging, auth, etc. middleware: app.use('/api/cat', cat) app.use('/api/dog', dog)
It is critical your custom routes are followed by the 'notFound' handler
app.use(notFound) and 'errorHandler'.
Up and Running
start script to your
"start": "node app.js", "start:dev": "nodemon app.js"
Then start the server via terminal:
npm start # Or npm run start:dev
Server will print the port it's listening on (default is 3000).
Override the port using the PORT environment variable:
# Optionally set PORT: PORT=5000 npm run start:dev
Use your browser or Postman to GET http://localhost:3000/api/cat
You should see a corresponding
200 status code in the terminal window running your server.
WARNING: CORS is configured to allow any website to make requests.
In production, the
originoption should be configured with the permitted domain name(s). To fix, update
app.jsto use an exact domain, e.g.: