Skip to content

vagnerzampieri/js-examples

Repository files navigation

Links

Examples

  • file notes.md has a lot of examples

Useful commands

  • see github.com/vagnerzampieri/cheatsheets/blob/master/javascript.md, there are commands to Node.js and npm.

Use exports without ES6

  • cd /example-1 && node index.js or node index2.js

Require with cache and without cache

  • cd /example-2 && node index2.js

Use ESM

  • cd /example-3 && node --experimental-modules index2.js

Run a server with HTTP

  • cd /example-4 && npm run dev-start

Run a server with Express and Nodemon (for reload automatically)

  • cd /example-5 && npm run dev-start

Use Promise, child_process example

  • cd /example-6 && npm run dev-start

Use EventEmitter

  • cd /example-7 && npm run dev-start

Express and MongoDB

  • Build a restful API with Node.js Express & MongoDB | Rest Api Tutorial - https://www.youtube.com/watch?v=vjf774RKrLc
  • cd /example-8
  • docker-compose up
  • cp .env.example .env put IP address in DB_CONTAINER
  • npm run dev-start
  • curl -d '{"title":"This is a title", "description":"This is a description"}' -H "Content-Type: application/json" -X POST http://localhost:3000/posts

Authentication with JWT

  • Build a Node.js API Authentication with JWT Tutorial - https://www.youtube.com/watch?v=2jqok-WgelI
  • cd /example-9
  • docker-compose up
  • cp .env.example .env put IP address in DB_CONTAINER and create a secret token for TOKEN_SECRET
  • npm run dev-start
  • curl -d '{"name":"Your name", "email":"[email protected]", "password":"123456"}' -H "Content-Type: application/json" -X POST http://localhost:3000/api/user/register
  • curl -d '{"email":"[email protected]", "password":"123456"}' -H "Content-Type: application/json" -X POST http://localhost:3000/api/user/login
  • curl -H "Content-Type: application/json" -H "Auth-Token: <TOKEN>" -X GET http://localhost:3000/api/users

Jest and Babel to use Node.js 12

Graphql

{
    author(id: 1) {
        name
        age
        books {
            name
        }
    }
}

{
    book(id: 1) {
        name
        genre
        authors {
            name
            age
        }
    }
}

mutation {
  addAuthor(name: "Homer Simpson", age: 42){
    name
    age
    id
  }
}

mutation {
  addBook(name: "My new book", genre: "bio", authorId: "5dc88adafead565ce4faa9bf") {
    name
    genre
    author {
      name
    }
  }
}

Redux

Static files with Express

  • cd example-13
  • npm run dev-start

Kafka

React Router 6.0

Node.js + TypeScript + tsx + tsup + vitest + zod

  • 4 libs que não podem faltar no Node.js com TypeScript (Setup Node.js + TypeScript) - https://www.youtube.com/watch?v=mxiRCcnsKDw
  • cd example-16
  • npm run start
  • npm run start:dev
  • npm run build
  • node dist/server.js
  • npm run test

Vanilla JS

Feed News - Nodejs + Typescript + Apollo Server + Graphql + Zod + MongoDB

  • cd example-18
  • npm run start:dev
query Queries {
  users {
    _id
    nickname
  }

  posts {
    _id
    content
    user_id
  }
}

{
  "post": {
    "content": "This is a content D",
    "user_id": "64ab5260b326d4e9b1070bf8"
  }
}

mutation Mutation($post: PostInput!) {
  createPost(post: $post) {
    _id
    user_id
    content
  }
}

Vite + Vanilla JS

Vite + Google Extension

Vite + Vitest + Typescript

  • cd example-21
  • npm run dev
  • npm run build
  • npm run preview
  • npm run test

Memory Game

Solo Ghost