├── .gitignore ├── .vscode ├── .gitignore └── launch.json ├── src ├── routes │ ├── studentRoute.js │ └── index.js ├── controllers │ └── studentController.js ├── app.js ├── services │ └── studentService.js └── database │ └── repositories │ └── studentRepository.js ├── package.json └── bin └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /.vscode/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /src/routes/studentRoute.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | const studentController = require('../controllers/studentController') 4 | 5 | router.get('/', studentController.get); 6 | 7 | module.exports = router; -------------------------------------------------------------------------------- /src/controllers/studentController.js: -------------------------------------------------------------------------------- 1 | const StudentService = require("../services/studentService"); 2 | 3 | exports.get = async (req, res, next) => { 4 | const payload = await new StudentService().getAllStudents(); 5 | res.status(200).send(payload); 6 | }; 7 | -------------------------------------------------------------------------------- /src/routes/index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | 4 | router.get("/", function (req, res, next) { 5 | res.status(200).send("Startse - Tech Academy - Módulo XI - Banco de Dados"); 6 | }); 7 | 8 | module.exports = router; 9 | -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | 4 | const index = require('./routes/index'); 5 | const studentRoute = require('./routes/studentRoute'); 6 | 7 | app.use(express.json()) 8 | app.use(express.urlencoded({ extended: true })) 9 | 10 | app.use('/', index); 11 | app.use('/student', studentRoute); 12 | 13 | module.exports = app; -------------------------------------------------------------------------------- /src/services/studentService.js: -------------------------------------------------------------------------------- 1 | const StudentRepository = require('../database/repositories/studentRepository.js'); 2 | 3 | class StudentService { 4 | constructor() { 5 | this.repository = new StudentRepository(); 6 | } 7 | 8 | async getAllStudents(){ 9 | return this.repository.findAll(); 10 | } 11 | }; 12 | 13 | module.exports = StudentService; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-express", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "src/app.js", 6 | "dependencies": { 7 | "express": "^4.15.4" 8 | }, 9 | "devDependencies": { 10 | "nodemon": "^1.17.3" 11 | }, 12 | "scripts": { 13 | "start": "nodemon ./bin/server.js" 14 | }, 15 | "keywords": [], 16 | "author": "Gustavo Viais", 17 | "license": "" 18 | } 19 | -------------------------------------------------------------------------------- /bin/server.js: -------------------------------------------------------------------------------- 1 | const app = require('../src/app'); 2 | 3 | const port = normalizaPort(process.env.PORT || '3000'); 4 | 5 | function normalizaPort(val) { 6 | const port = parseInt(val, 10); 7 | if (isNaN(port)) { 8 | return val; 9 | } 10 | 11 | if (port >= 0) { 12 | return port; 13 | } 14 | 15 | return false; 16 | } 17 | 18 | app.listen(port, function () { 19 | console.log(`app listening on port ${port}`) 20 | }) -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible Node.js debug attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${file}" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /src/database/repositories/studentRepository.js: -------------------------------------------------------------------------------- 1 | class StudentRepository { 2 | async findAll() { 3 | const alunos = [ 4 | { 5 | code: 12345, 6 | name: 'José da Silva', 7 | dob: '2000-07-09' 8 | }, 9 | { 10 | code: 12346, 11 | name: 'Joana Souza', 12 | dob: '1998-01-11' 13 | }, 14 | { 15 | code: 12347, 16 | name: 'Lucas Gomes', 17 | dob: '1992-07-17' 18 | }, 19 | { 20 | code: 12348, 21 | name: 'Maria Eduarda', 22 | dob: '2001-02-09' 23 | }, 24 | ]; 25 | return alunos; 26 | } 27 | } 28 | 29 | module.exports = StudentRepository; 30 | --------------------------------------------------------------------------------