Constantly improving and updating. Because I use it on my own projects.
This Boilerplate need postgreSQL on database operations.
- Clone the project
git clone https://github.com/dizefurkan/node-api-boilerplate.git
. - Install dependencies
npm install
- You need to install PostgreSQL db. How to?
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'
}
};
- express
- nodemon
- body-Parser
- morgan
- cors
- joi
- pg
- pg-hstore
- sequelize
- sequelize-cli
- serve-static
- jsonwebtoken
- babel-cli
- babel-eslint
- babel-plugin-module-resolver
- babel-preset-env
- babel-preset-stage-0
- eslint
- eslint-config-airbnb-base
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 |
/* 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);
}
});
/* 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;
/* 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;
//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()
})
};
For eslint control
npm run lint
Server running on http://localhost:3030/
/* src/config/server.js */
export default {
port: 3030
};