DEV Community

Dwi purnomo
Dwi purnomo

Posted on • Updated on


Example REST API with Express.js, Mongoose and Babel

Read on my blog.

This article will discuss an example of making a REST API using Express.js, Mongoose and Babel. The resource or endpoint created is only users. We will only make a simple user registration method. We will use a repository pattern.

  • Install the package
  yarn add express mongoose mongod
  yarn add --dev @babel/cli @babel/core @babel/node @babel/preset-env nodemon
  • Create folder structure
├── package.json
├── src
│   ├── controllers
│   │   └── UserController.js
│   ├── index.js
│   ├── models
│   │   ├── repositories
│   │   │   └── UserRepository.js
│   │   └── User.js
│   └── routes
│       ├── index.js
│       └── users.js
└── yarn.lock

5 directories, 8 files
  • User Model User.js
import mongoose from 'mongoose';

const { Schema } = mongoose;

const schema = new Schema({
  email: {
    type: String
  password: {
    type: String

const User = mongoose.model('User', schema);
export default User;
  • User Repository UserRepository.js

Create a repositories folder in the models folder, then create UserRepository.js

import User from '../User';

class UserRepository {

  constructor(model) {
    this.model = model;

  create(object) {
    return this.model.create(object);

export default new UserRepository(User);

Then create UserController.js in the controllers folder.

  • User Controller UserController.js
import UserRepository from '../models/repositories/UserRepository';

function createUser(req, res) {
  const user = req.body;

    .then((newUser) => {
    }).catch((errors) => {

export default { createUser };

After that we list the routes in the routes folder as users.js

  • User Route users.js
import express from 'express';
import UserController from '../controllers/UserController';

const router = express.Router();'/', UserController.createUser);

export default router;

And index.js on routes

import express from 'express';
import users from './users';

const router = express.Router();


router.use('/users', users);

export default router;

Finally we create index.js in the src

  • Server index.js
import express from 'express';
import mongoose from 'mongoose';

import routes from './routes';

mongoose.Promise = global.Promise;

const app = express();

app.use('/', routes);

  .then(() => {
    console.log('mongodb started.');
    app.listen(8000, () => {
      console.log('Server started on 8000');
  }).catch(() => {
    console.log('Mongodb connection failed.');
  • Command to run the server
nodemon -w src -x "babel-node src --presets @babel/env"

We can add it to npm-scripts in package.json

  "name": "exercise",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1",
    "mongodb": "^3.2.7",
    "mongoose": "^5.6.2",
    "nodemon": "^1.19.1"
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/preset-env": "^7.4.5"
  "scripts": {
    "start": "nodemon -w src -x \"babel-node src --presets @babel/env\""

That was practicing express.js, mongoose and babel using a simple REST API. The entire code can be seen at

Top comments (0)

The AI Brief

AI generated git commit messages

Minimize the struggle of remembering what you just coded. AI-generated commits make it easier to manage projects and keep track of changes. The Nutlope/aicommits project demonstrates how AI can improve commit messages.

I open sourced an AI that creates any UI in seconds

Make AI-generated user interfaces a breeze. This open-source project harnesses the power of generative AI technologies like chatGPT to create versatile, quick, and intuitive UI components.

Use AI to commit like a PRO in 1 second

Upgrade your commit message game with AI. Boost your productivity by using ChatGPT to generate commit messages and avoid context switching. OpenCommit is an open-source library that helps you achieve this easily.

Build your own ChatGPT starter kit

Train AI models on custom data for improved domain-specific knowledge. Combine the power of WebView technologies and this starter kit to train your ChatGPT model on specific websites, allowing for better-optimized outcomes.