SHARE
TWEET

Scripts

a guest May 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### index.js
  2. const config = require('./config/keys.config')
  3.  
  4. const server = require('./server')(config)
  5.  
  6. server.listen(config.app.PORT, '0.0.0.0', error => {
  7.   if (error) {
  8.     console.log(`Erro na Inicialização da Aplicação\nError Message ${error.message}`)
  9.   } else {
  10.     console.log(`Aplicação Inicializada com Sucesso.\nPara Utilizar Acesse a URL: ${config.app.URL}`)
  11.   }
  12. })
  13.  
  14.  
  15. ### server/index.js
  16. const server = config => {
  17.   const express = require('express')
  18.   const http = require('http')
  19.  
  20.   const socket = require('./socketIo')
  21.   const middlewares = require('./middlewares')
  22.   const routes = require('../routes')
  23.  
  24.   const app = express()
  25.   const server = http.Server(app)
  26.  
  27.   socket({ app, server })
  28.   middlewares({ app, express, config })
  29.   routes(app)
  30.  
  31.   return server
  32. }
  33.  
  34. module.exports = server
  35.  
  36.  
  37. ### server/socketIo
  38. const socket = require('socket.io')
  39.  
  40. const socketSrv = deps => {
  41.   const { app, server } = deps
  42.   const io = socket(server)
  43.  
  44.   io.on('connection', socket => {
  45.     socket.on('connectRoom', notification => {
  46.       socket.join(notification)
  47.     })
  48.   })
  49.  
  50.   app.use((req, res, next) => {
  51.     req.io = io
  52.  
  53.     return next()
  54.   })
  55.  
  56.   return app
  57. }
  58.  
  59. module.exports = socketSrv
  60.  
  61.  
  62. ### server/middlewares
  63. const helmet = require('helmet')
  64. const cors = require('cors')
  65. const logger = require('morgan')
  66.  
  67. const jwt = require('./jwtMiddleware')
  68.  
  69. const middlewares = deps => {
  70.   const { app, express, config } = deps
  71.  
  72.   app.use(cors())
  73.   app.use(logger('combined'))
  74.   app.use(express.json())
  75.   app.use(express.urlencoded({ extended: true }))
  76.   app.use(helmet.frameguard())
  77.   app.use(helmet.xssFilter())
  78.   app.use(helmet.noSniff())
  79.   app.use(helmet.ieNoOpen())
  80.   app.use(jwt(config))
  81.   app.disable('x-powered-by')
  82. }
  83.  
  84. module.exports = middlewares
  85.  
  86.  
  87. ### routes/index.js
  88. const authentication = require('./modules/auth.routes')
  89. const users = require('./modules/users.routes')
  90.  
  91. const routes = app => {
  92.   app.get('/', function (req, res) {
  93.     res.status(200).json({ message: 'YEAH! Seja Bem-Vindo a nossa API' })
  94.   })
  95.  
  96.   authentication(app)
  97.   users(app)
  98. }
  99.  
  100. module.exports = routes
  101.  
  102.  
  103. ### routes/modules/auth.routes.js
  104. const auth = (app) => {
  105.   app.get('/auth/login', function (req, res, next) {
  106.     try {
  107.       res.status(200).json({ message: 'Authenticação da API - Login Efetuado com Sucesso' })
  108.     } catch (error) {
  109.       res.json({ error: error.message })
  110.     }
  111.     next()
  112.   })
  113. }
  114.  
  115. module.exports = auth
  116.  
  117.  
  118. ### routes/modules/users.routes.js
  119. const users = (app) => {
  120.   app.get('/user', async (req, res, next) => {
  121.     try {
  122.       res.status(200).json({ message: 'Usuários - Listagem de Todos os Usuários' })
  123.     } catch (error) {
  124.       res.status(422).json({ error: error.message })
  125.     }
  126.     next()
  127.   })
  128. }
  129.  
  130. module.exports = users
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top