Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

dizefurkan/node-api-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS Boilerplate

forthebadge forthebadge

Get Started

Constantly improving and updating. Because I use it on my own projects.

Build

This Boilerplate need postgreSQL on database operations.

Installation

  1. Clone the project git clone https://github.com/dizefurkan/node-api-boilerplate.git.
  2. Install dependencies npm install
  3. You need to install PostgreSQL db. How to?

Install PostgreSQL

https://www.postgresql.org/download/

module.exports = {
  development: {
    url: 'postgres://postgres:12345@localhost:5432/nodeapi',
    dialect: 'postgres'
  },
  test: {
    url: process.env.database || 'postgres://postgres:12345@localhost:5432/nodeapi',
    dialect: 'postgres'
  },
  production: {
    url: process.env.database || 'postgres://postgres:12345@localhost:5432/nodeapi',
    dialect: 'postgres'
  }
};

Techs


Project Structure

Folder Description
data/migrations The migration folder is here for PostgreSQL
public For public folder
src Source folder
src/config For config folders
src/constants You can keep here like replies file
src/core This folder for application core files
src/library Put files here like dboperations, cryptop operations...
src/middlewares This folder for middlewares
src/models This folder for PostgreSQL
src/services This folder for Services

Code Examples

App.js

/* src/app.js */
import express from 'express';
import { server } from 'config';
import expressCore from 'core/express';

const app = express();

expressCore.forEach(item => app.use(item));

app.listen(server.port, (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('Server listening at localhost:%s', server.port);
  }
});

Routers

/* src/core/router.js */
import express from 'express';
import services from 'services';
import middlewares from 'middlewares';

const app = express();

middlewares.forEach(item => {
  app.use(item);
});

services.forEach(item => {
  app[item.method](item.path, item.handler);
});

export default app;

Services

/* src/services/index.js */
import fs from 'fs';
const services = [];

fs
  .readdirSync(__dirname)
  .filter(file => ((file.indexOf('.') !== 0) && (file !== 'index.js')))
  .forEach((file) => {
    const service = require('./'   file).default; // eslint-disable-line
    service.forEach(item => services.push(item));
  });

export default services;

Login Services and Joi

//services/auth/index.js
import validater from './validater';
{
    method: 'post',
    path: '/login',
    handler: async (req, res) => {
      try {
        await Joi.validate(req.body, validater.login);
        return res.send({
          success: true,
          message: 'Done'
        });
      } catch (err) {
        return res.send(err);
      }
    }
}
/* src/services/auth/validater.js */
import Joi from 'joi';

export default {
  login: Joi.object().keys({
    email: Joi.string().email().required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]/)
      .min(8)
      .max(30)
      .required()
  })
};

Scripts

DEV

For eslint control

npm run lint

Server running on http://localhost:3030/

/* src/config/server.js */
export default {
  port: 3030
};

Authors


Said Furkan Dize