├── src ├── v1 │ ├── logs │ │ ├── error.log │ │ └── info.log │ ├── controllers │ │ └── index.controller.js │ ├── middlewares │ │ └── index.middleware.js │ ├── rests │ │ └── test.rest.http │ ├── routes │ │ └── index.router.js │ ├── services │ │ └── redis.service.js │ ├── databases │ │ ├── init.redis.js │ │ └── init.mongodb.js │ └── utils │ │ └── logger.js └── app.js ├── .gitignore ├── .env ├── server.js ├── ecosystem.config.js ├── README.md └── package.json /src/v1/logs/error.log: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/v1/logs/info.log: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /src/v1/controllers/index.controller.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/v1/middlewares/index.middleware.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | PORT=3051 2 | MONGO_URI=mongodb://localhost:27017/admin?authSource=admin 3 | REDIS_URL=localhost -------------------------------------------------------------------------------- /src/v1/rests/test.rest.http: -------------------------------------------------------------------------------- 1 | @urlDev=http://localhost:3051 2 | 3 | ### checkStatus 4 | 5 | GET {{urlDev}}/checkstatus -------------------------------------------------------------------------------- /src/v1/routes/index.router.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | 4 | router.get('/checkstatus', (req, res, next) => { 5 | res.status(200).json({ 6 | status: 'success', 7 | message: 'api ok' 8 | }) 9 | }) 10 | 11 | module.exports = router; -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const app = require('./src/app') 3 | 4 | 5 | 6 | const {PORT} = process.env; 7 | 8 | 9 | 10 | 11 | const server = app.listen( PORT, () => { 12 | console.log(`WSV start with port ${PORT}`); 13 | }) 14 | 15 | process.on('SIGINT', () => { 16 | server.close( () => console.log(`exits server express`)) 17 | }) 18 | -------------------------------------------------------------------------------- /src/v1/services/redis.service.js: -------------------------------------------------------------------------------- 1 | const client = require('../databases/init.mongodb') 2 | const { promisify } = require("util") 3 | 4 | const REDIS_GET = promisify(client.get).bind(client); 5 | const REDIS_SET = promisify(client.set).bind(client); 6 | const REDIS_LRANGE = promisify(client.lrange).bind(client); 7 | 8 | 9 | module.exports = { 10 | REDIS_GET, 11 | REDIS_SET, 12 | REDIS_LRANGE 13 | } -------------------------------------------------------------------------------- /src/v1/databases/init.redis.js: -------------------------------------------------------------------------------- 1 | const { createClient } = require('redis'); 2 | const client= createClient({ 3 | url: process.env.REDIS_URL 4 | }); 5 | client.ping(function (err, result) { 6 | console.log(result); 7 | }) 8 | 9 | client.on('connect', () => { 10 | console.log('Redis client connected'); 11 | }); 12 | 13 | client.on("error", (error) => { 14 | console.error(error); 15 | }); 16 | 17 | module.exports = client -------------------------------------------------------------------------------- /ecosystem.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | apps: [{ 3 | name: "wsv-anonystick", 4 | script: "./server.js", 5 | exec_mode: 'cluster', 6 | instances: 4, 7 | watch: true, 8 | ignore_watch: ["node_modules"], 9 | env_production: { 10 | NODE_ENV: "production" 11 | }, 12 | env_development: { 13 | NODE_ENV: "development" 14 | } 15 | }] 16 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # express-mvc-framework 2 | nodejs + express + mongodb to do MVC example 3 | 4 | Youtube: [API Structure Your Nodejs REST API for beginner to Advanced](https://youtu.be/i4Pr81apfnU) 5 | 6 | Blog: [backend nodejs](https://anonystick.com) 7 | 8 | # How to use 9 | 10 | ### clone git 11 | 12 | ``` 13 | > git clone https://github.com/anonystick/structure-api-mvc-express-nodejs.git your-project 14 | > cd your-project 15 | > npm i 16 | > npm run dev 17 | ``` 18 | 19 | # Try 20 | 21 | [localhost](http://localhost:3051) 22 | -------------------------------------------------------------------------------- /src/v1/databases/init.mongodb.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | //connect mongoose 4 | mongoose.connect( process.env.MONGO_URI).then( _ => console.log('Connected mongoose success!...')) 5 | .catch( err => console.error(`Error: connect:::`, err)) 6 | 7 | // all executed methods log output to console 8 | mongoose.set('debug', true) 9 | 10 | // disable colors in debug mode 11 | mongoose.set('debug', { color: false }) 12 | 13 | // get mongodb-shell friendly output (ISODate) 14 | mongoose.set('debug', { shell: true }) 15 | 16 | 17 | module.exports = mongoose; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "structure-api-mvc-express-nodejs", 3 | "version": "1.0.0", 4 | "description": "structure api mvc express nodejs", 5 | "main": "index.js", 6 | "directories": { 7 | "doc": "docs" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1", 11 | "dev": "nodemon server.js" 12 | }, 13 | "keywords": [], 14 | "author": "anonystick ", 15 | "license": "ISC", 16 | "dependencies": { 17 | "compression": "^1.7.4", 18 | "dotenv": "^16.0.1", 19 | "express": "^4.18.1", 20 | "helmet": "^5.1.0", 21 | "mongoose": "^6.3.3", 22 | "morgan": "^1.10.0", 23 | "nodemon": "^2.0.20", 24 | "redis": "^3.1.2", 25 | "winston": "^3.7.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/v1/utils/logger.js: -------------------------------------------------------------------------------- 1 | const { createLogger, format, transports } = require("winston"); 2 | module.exports = createLogger({ 3 | format: format.combine( 4 | format.timestamp({ format: "MMM-DD-YYYY HH:mm:ss" }), 5 | format.align(), 6 | format.printf((i) => `${i.level}: ${[i.timestamp]}: ${i.message}`) 7 | ), 8 | transports: [ 9 | new transports.File({ 10 | filename: "logs/info.log", 11 | level: "info", 12 | format: format.combine( 13 | format.printf((i) => 14 | i.level === "info" ? `${i.level}: ${i.timestamp} ${i.message}` : "" 15 | ) 16 | ), 17 | }), 18 | new transports.File({ 19 | filename: "logs/error.log", 20 | level: "error", 21 | }), 22 | ], 23 | }) -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const helmet = require('helmet') 4 | const morgan = require('morgan') 5 | const compression = require('compression') 6 | 7 | //init dbs 8 | // require('./v1/databases/init.mongodb') 9 | // require('./v1/databases/init.redis') 10 | 11 | //user middleware 12 | app.use(helmet()) 13 | app.use(morgan('combined')) 14 | // compress responses 15 | app.use(compression()) 16 | 17 | // add body-parser 18 | app.use(express.json()) 19 | app.use(express.urlencoded({ 20 | extended: true 21 | })) 22 | 23 | //router 24 | app.use(require('./v1/routes/index.router')) 25 | 26 | // Error Handling Middleware called 27 | 28 | app.use((req, res, next) => { 29 | const error = new Error("Not found"); 30 | error.status = 404; 31 | next(error); 32 | }); 33 | 34 | 35 | // error handler middleware 36 | app.use((error, req, res, next) => { 37 | res.status(error.status || 500).send({ 38 | error: { 39 | status: error.status || 500, 40 | message: error.message || 'Internal Server Error', 41 | }, 42 | }); 43 | }); 44 | 45 | module.exports = app; --------------------------------------------------------------------------------