├── README.md ├── app.js ├── data └── noticias.json ├── package.json └── src ├── controllers └── news.js ├── models └── news.js ├── routes └── news.js └── views └── news └── index.ejs /README.md: -------------------------------------------------------------------------------- 1 | # MVC Exemplo Node.JS 2 | Projeto criado para exemplo prático do artigo: MVC - Conceito e exemplo em Node.JS 3 | 4 | Artigo disponível em meu blog: https://irias.com.br/blog/mvc-conceito-e-exemplo-em-node-js/ 5 | 6 | ### Dependências 7 | 8 | * Node.js 9 | * Site oficial: https://nodejs.org/en/ 10 | * NPM 11 | * Site oficial: https://www.npmjs.com/ 12 | 13 | ### Utilização 14 | 15 | Após clonar o projeto, acesse o diretório onde se encontra o arquivo **package.json** e utilize o comando **npm install --save** para instalar as dependências do projeto. 16 | 17 | Depois utilize o comando **node app.js** para iniciar a aplicação. 18 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var consign = require('consign'); 3 | 4 | var app = express(); 5 | app.set('view engine', 'ejs'); 6 | app.set('views', './src/views'); 7 | 8 | consign() 9 | .include('src/routes') 10 | .then('src/models') 11 | .then('src/controllers') 12 | .into(app); 13 | 14 | app.listen(3000, function(){ 15 | console.log('APP rodando na porta 3000'); 16 | }); 17 | 18 | -------------------------------------------------------------------------------- /data/noticias.json: -------------------------------------------------------------------------------- 1 | { 2 | "noticias": [ 3 | { 4 | "titulo": "Primeira notícia", 5 | "noticia": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 6 | }, 7 | { 8 | "titulo": "Segunda notícia", 9 | "noticia": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 10 | } 11 | 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mvc-exemplo-nodejs", 3 | "version": "1.0.0", 4 | "description": "Exemplo prático do artigo: \"MVC - Conceito e exemplo em Node.JS\"", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Anderson Irias", 10 | "license": "ISC", 11 | "dependencies": { 12 | "consign": "^0.1.6", 13 | "ejs": "^2.6.1", 14 | "express": "^4.16.4", 15 | "fs": "0.0.1-security" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/controllers/news.js: -------------------------------------------------------------------------------- 1 | module.exports.index = function(application, req, res) { 2 | var newsModel = new application.src.models.news(); 3 | 4 | newsModel.getLastNews(function(err, result) { 5 | res.render("news/index", {news : result}); 6 | }); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /src/models/news.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | 3 | function news() {} 4 | 5 | news.prototype.getLastNews = function(callback) { 6 | fs.readFile('./data/noticias.json', 'utf8', function(err, result) { 7 | var data = []; 8 | 9 | if (!err) { 10 | var obj = JSON.parse(result); 11 | 12 | if (obj.noticias.length > 4) { 13 | var i = 4; 14 | } else { 15 | var i = (obj.noticias.length - 1); 16 | } 17 | 18 | obj.noticias.forEach(function(noticia) { 19 | if (i >= 0) { 20 | data[i] = noticia; 21 | i--; 22 | } 23 | }); 24 | } 25 | callback(err, data); 26 | }); 27 | }; 28 | 29 | module.exports = function(){ 30 | return news; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/routes/news.js: -------------------------------------------------------------------------------- 1 | module.exports = function(application){ 2 | application.get('/', function(req, res){ 3 | application.src.controllers.news.index(application, req, res); 4 | }); 5 | } 6 | 7 | -------------------------------------------------------------------------------- /src/views/news/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | MVC Exemplo Node.JS 7 | 8 | 9 | 10 |
11 |


12 |

MVC Exemplo Node.JS

13 |
14 | <% if (news.length > 0) { %> 15 |
16 | <% for(var i = 0; i < news.length; i++) {%> 17 |
18 |

<%= news[i].titulo %>

19 |

<%= news[i].noticia %>

20 |
21 | <% } %> 22 |
23 | <% } %> 24 |
25 | 26 | 27 | --------------------------------------------------------------------------------