Create a Mongo/Node API in 10 minutes!

Using Heroku, you can get the barebones of a Node/Mongo API in just under 10 minutes!

    Prerequisites

  • A registered Heroku account
  • Install Heroku CLI
  • Access to your Heroku dashboard
  • Step 1 – Create and navigate to project directory

    Wherever you want your project to be. Perhaps c:\My Cool Things\API or /Users/hank/projects/api-project.

    Step 1 – Create Heroku app

    From the command line of your choice, from your project directory:

    $ heroku create

    You’ll be presented with a nifty server name. Something like falling-wind-1234 or similarly zen-like.

    Step 2 – Create app structure

    For a barebones API you’ll need just just 3 basic things.

    2a – Procfile
    Needed for Heroku to run the app. The contents of this file is just one line for now.

    web: node index.js

    2b – package.js
    This manages your node packages and will be managed automatically via your

    $ npm install

    commands you make later.

    Type

    $ npm init

    and hit enter through all of the prompts. The default values will be fine for now.

    2c – index.js
    The starting point of your app will start and connect to your mongo/node server. We will create this in Step 4!

    Step 3 – Install dependencies

    Since this is a barebone API, we will have just 3 dependencies: mongodb and hapijs

    Back at the command line, type

    $ npm install mongodb --save
    $ npm install hapijs --save
    $ npm install mongoose --save

    Step 4 – Create index.js

    const Hapi = require('hapi');
    const Mongoose = require('mongoose');
    
    // Create a server with a host and port
    const server = new Hapi.Server();
    
    var mongoUri = process.env.MONGODB_URI || 'mongodb://YOUR_MONGO_URI';
    var serverPort = process.env.PORT || 8080;
    
    Mongoose.connect(mongoUri, function(err) {
    	if (err) {
    		console.log(err);
    		process.exit(1);
    	}
    
    	server.connection({
    		port: serverPort
    	});
    
    	// GET locations route
    	server.route({
    		method: 'GET',
    		path:'/hello',
    		handler: function (request, reply) {
    			return reply("Hello, this is my API");
    		}
    	});
    
    	// Start the server
    	server.start((err) => {
    
    		if (err) {
    			throw err;
    		}
    		console.log('Server running at:', server.info.uri);
    	});
    });
    
    

    Step 5 – Deploy!

    To send it up to your Heroku app, type

    $ git commit -am "My 10 minute API!"
    $ git push heroku

    After the app is built, you can navigate to your Heroku app url (for example, https://guarded-bear-24888.herokuapp.com/) and see the results!

    Using our example, navigating to just the base url will produce a 404, but a valid response nonetheless! If you navigate to the base url followed by /hello, you should get our hello world response.

    Next steps for your newly minted API could be to create some routes that interact with your mongo instance, make some hapijs views or publish your code to your personal github repo.

Leave a Reply

Your email address will not be published. Required fields are marked *