DEV Community

Cover image for Build and publish an api quickly with express.
Beaudry Jean Sauvenel
Beaudry Jean Sauvenel

Posted on

Build and publish an api quickly with express.

Today when you plan to build an api many things come in your head like you say why not use a serverless platform like Firebase, AWS, Stich etc. Because build an api can take some time to:

1- Identify the entities
2- Manage the relationship between them
3- Plan the routes
4- Authentification
5- Security
6- Etc.

In this article i am going to show you how you can build an Api easily. To start we will use node.js with express as the main tools.

The first thing you need to do is identify our entities by example we will do a api for a blog.

As entities we have : users, articles, comments

Now let's start:

1 - Download and install node if not yet done.
link: https://nodejs.org/en/

2 - Install Yoeman,
Yeoman helps you to kickstart new projects, prescribing best
practices and tools to help you stay productive.

  $ npm install -g yo

3 - Install generator-api
Generator for creating RESTful NodeJS APIs, using ES6,
Mongoose and Express. The fastest way to get your project up and
running using an awesome stack.

  $ npm install -g generator-api

4 - Ok, once done type yo api and answer the questions

  $ yo api
  ? Server name: blog-api
  ? Server description: Api to manage my blog
  ? Server version: 1.0.0
  ? Author name: Jean Sauvenel Beaudry
  ? Author email: sauvene2013@gmail.com
  ? Models: (singular and comma separated) users,articles,comments
  ? what should the database be named? mongo_string_db
  ? would you like to have Docker included in the app? No
  create package.json
  create config.js
  create index.js
  create routes.js
  create .gitignore
  create .eslintrc.json
  create .eslintignore
  create lib/controller.js
  create lib/facade.js
  create model/users/controller.js
  create model/users/facade.js
  create model/users/router.js
  create model/users/schema.js
  create model/articles/controller.js
  create model/articles/facade.js
  create model/articles/router.js
  create model/articles/schema.js
  create model/comments/controller.js
  create model/comments/facade.js
  create model/comments/router.js
  create model/comments/schema.js
  create README.md 

5 - Alright our blog is created. To test it we need to connect the api with a database. To do that we are going to use MongoDB

MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schema. It's a super database platform to start. Go on the link to create a mongo database instance like that:

mongodb+srv://user:passwordx@cluster.mongodb.net/database_name?retryWrites=true&w=majority

Link: https://www.mongodb.com

6 - Go to config.js replace mongodb://localhost/mongo_string_db with your new mongodb string or use a .env file to affect MONGO_DB_URI (recommend for more secure).

   const config = {
   environment: process.env.NODE_ENV || 'dev',
   server: {
   port: process.env.PORT || 8080
   },
   mongo: {
   url: process.env.MONGO_DB_URI || 
   'mongodb://localhost/mongo_string_db'
   }
  }

7 - Now we are ready to go. Just type

  $ npm start  or yarn start

You can use postman test your CRUD.

Next articles:
1 - Secure this Api.
2 - Authenticate your users
3 - Grow it by adding new model
6 - Deploy with a single command line using Zeit (Now)

Thank you for reading. My current project : https://www.snipplabs.com/

Top comments (0)