Giter Site home page Giter Site logo

api-node-rocketseat's Introduction

Passos para criar API em Node.js

Duas opções:

A mais fácil: Você pode clonar esse repositório e rodar

yarn install

A mais legal: Seguir o passo a passo abaixo

estrutura

pastas
  • src
  • src/app
  • src/app/controllers
  • src/app/models
  • src/config
  • src/database
  • src/database/migrations
arquivos
  • src/app.js
  • src/server.js
  • src/routes.js
  • src/config/database.js

comandos iniciais

  • yarn init -y
  • yarn add express
  • yarn add sucrase nodemon -D
  • yarn add eslint -D
  • yarn add sequelize -D
  • yarn add sequelize-cli -D
  • yarn add pg pg-hstore
  • yarn add bcryptjs (opcional para aplicar hash em senhas)
  • yarn add jsonwebtoken (opcional para trabalhar com autenticação JWT)

package.json

Adicionar:

"scripts" : {
  "dev" : "nodemon src/server.js"
},

nodemon

Criar arquivo nodemon.json na raiz do projeto

{
  "execMap" : {
    "js" : "sucrase-node"
  }
}

eslint

Requer extensão ESLint (dbaeumer.vscode-eslint) instalada no VSCode

yarn eslint --init

Selecionar as seguintes opções:

  • To check syntax, find problems, and enforce code style
  • Javascript modules (import/export)
  • None of These
  • Browser
  • Node
  • Use a popular style guide
  • Airbnb
  • Javascript
  • Y

Deletar arquivo package-lock.json e atualizar dependencias do yarn com o comando:

yarn

Open Settings (JSON) - VSCode (CTRL+Shift+P)

Adicionar:

"editor.formatOnSave": true,
"eslint.autoFixOnSave": true,
"eslint.validate": [
  {
    "language": "javascript",
    "autoFix": true
  },
  {
    "language": "javascriptreact",
    "autoFix": true
  },
  {
    "language": "typescript",
    "autoFix": true
  },
  {
    "language": "typescriptreact",
    "autoFix": true
  }
]

.eslintrc.js

Adicionar:

rules: {
  'prettier/prettier': 'error',
  'class-methods-use-this': 'off',
  'no-param-reassign': 'off',
  camelcase: 'off',
  'no-unused-vars': ['error', { argsIgnorePattern: 'next' }],
},

prettier

Executar comando:

yarn add prettier eslint-config-prettier eslint-plugin-prettier -D

.eslintrc.js

Alterar:

extends: ['airbnb-base', 'prettier'],
plugins: ['prettier'],

Criar arquivo .prettierrc na raiz do projeto

Contendo:

{
  "singleQuote": true,
  "trailingComma": "es5"
}

Dica: para realizar fix automatico em todos arquivos JS da pasta SRC:

yarn eslint --fix src --ext .js

editorconfig

Requer extensão EditorConfig (editorconfig.editorconfig) instalada no VSCode

Criar arquivo .editorconfig na raiz do projeto (ou clicar com botão direito do mouse em cima da pasta do projeto no VSCode e selecionar a opção 'Generate .editorconfig)

Criar ou alterar com:

root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

sequelize

Criar arquivo .sequelizerc na raiz do projeto.

Contendo:

const { resolve } = require('path');

module.exports = {
  config: resolve(__dirname, 'src', 'config', 'database.js'),
  'models-path': resolve(__dirname, 'src', 'app', 'models'),
  'migrations-path': resolve(__dirname, 'src', 'database', 'migrations'),
  'seeds-path': resolve(__dirname, 'src', 'database', 'seeds'),
};

Configure o arquivo src/config/database.js

module.exports = {
  dialect: '<tipo de banco, ex: postgres, mysql>',
  host: '<host, ex: localhost>',
  username: '<seu username>',
  password: '<sua senha>',
  database: '<seu banco de dados>',
  define: {
    timestamps: true,
    undescored: true,
    undescoredAll: true,
  },
};

Isso é tudo pessoal!

api-node-rocketseat's People

Contributors

casonwebdev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.