This is a basic express project to help build API's with MongoDB quickly. It's so easy a baby could (maybe) do it. I'll take you through going from 0 to a Heroku (or Dokku) Deployed API in 5-10 mins.
(heroku free instance, might be sleeping...) https://foo-api.herokuapp.com/
To the right of MongoDB Deployments select Create New
. Make sure you are not creating a New Private Environment.
Then select your favorite (or a random) Cloud Provider.
Select Sandbox (cause its free!). Then click Continue.
Pick a region near you.
Name your DB.
Submit order.
Click your database that you just made.
Click User
, then Add Database User
.
Type a username and password.
Copy the second URL given at the top!
To connect using a driver via the standard MongoDB URI (what's this?):
mongodb://<dbuser>:<dbpassword>@ds123456.mlab.com:12345/<your-db-name>
You will need this URL for the next step.
cp .env.example .env
Use your favorite (or least favorite) editor to add your MongoDB connection URL
vim .env
Now paste that URL next to MONGO_URL
MONGO_URL=mongodb://<dbuser>:<dbpassword>@ds123456.mlab.com:12345/<your-db-name>
But make sure you type in the username and password you created for your database into that URL (NOT YOUR MLAB ACCOUNT LOGIN)
git clone https://github.com/paineleffler/express-mongo-boilerplate.git
cd express-mongo-boilerplate
if you really really want nodemon:
npm install -g nodemon
npm install
npm run dev
npm start
var sesh = {
name: 'insert cool name here',
secret: 'not a keyboard cat',
//...
}
const port = 5000;
if (app.get('env') === 'production') {
//...
sesh.cookie.domain = 'insert.domain.here.com';
//...
}
In your app/routes/foo_routes.js
add this to the top:
var cors = require('cors')
var corsOptions = {
origin: 'https://domain.calling.api.com'
}
Then configure your routes with CORS by adding cors(corsOptions)
:
//example
app.get('/example', cors(corsOptions) (req, res) => {
//...
});
Make sure you have the Heroku CLI:
https://devcenter.heroku.com/articles/heroku-cli
run heroku create <crazy-unique-name-here>
Then this should Echo back:
Creating ⬢ <crazy-unique-name-here>... done
https://<crazy-unique-name-here>.herokuapp.com/ | https://git.heroku.com/<crazy-unique-name-here>.git
Make sure to set your MONGODB URL or it will not work!!!
heroku config:set MONGO_URL=mongodb://<dbuser>:<dbpass>@<ds123456>.mlab.com:<12345>/<dbname>
Make sure you git add && git commit -a -m 'commit message'
Then git push heroku master
or whatever branch you like deploying from
Then BAMMMM it's on Heroku! Go look at:
https://crazy-unique-name-here.herokuapp.com/
Note your free heroku instance will sleep after 30 minutes of no activity.
Add your dokku remote to your .git/config
[remote "dokku"]
url = dokku@<dokku-server-ip-address>:api
fetch = +refs/heads/*:refs/remotes/dokku/*
Set Dokku Environment Variable
dokku config:set MONGO_URL=mongodb://<dbuser>:<dbpass>@<ds123456>.mlab.com:<12345>/<dbname>
Then git push dokku master
BOOM!
cp app/routes/foo_routes.js app/routes/bar_routes.js
Add new app/routes/bar_routes.js
to the app/routes/index.js
const fooRoutes = require('./foo_routes');
const barRoutes = require('./bar_routes');
module.exports = function(app, db) {
fooRoutes(app, db);
barRoutes(app, db);
};
Customize your app/routes/bar_routes.js
as you wish...
Params should be sent as x-www-form-urlencoded in the body.
get /
root route of the API
get /foo
fetches all the foo's
get /foo/:id
fetches a foo by id
post /foo
creates a foo
delete /foo/:id
delete a foo by id
put /foo/:id
update a foo
coming soon