Skip to content

Instantly share code, notes, and snippets.

@renatosousafilho
Created June 2, 2021 22:31
Show Gist options
  • Save renatosousafilho/687caef1869b2714ada49c3847efd356 to your computer and use it in GitHub Desktop.
Save renatosousafilho/687caef1869b2714ada49c3847efd356 to your computer and use it in GitHub Desktop.
Snippets para Node + Express
{
// Place your snippets for javascript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"hello-world": {
"prefix": "helloworld",
"body": [
"console.log('hello world');",
],
"description": "Log output to console"
},
"mongodb-connection": {
"prefix": ["mongodb-connection"],
"body": [
"const { MongoClient } = require('mongodb');",
"",
"const MONGODB_URL = 'mongodb://127.0.0.1:27017';",
"const DATABASE = '';",
"",
"const connection = () => {",
" return MongoClient.connect(MONGODB_URL, {",
" useNewUrlParser: true,",
" useUnifiedTopology: true",
" })",
" .then((conn) => conn.db(DATABASE))",
" .catch((err) => {",
" process.exit();",
" });",
"}",
"",
"module.exports = connection;"],
"description": "Mongodb connection"
},
"mysql2-connection": {
"prefix": [
"mysql2-connection"
],
"body": [
"const mysql = require('mysql2/promise')",
"",
"const connection = mysql.createPool({",
" user: 'root',",
" password: '',",
" host: 'localhost',",
" database: 'books_api'",
"});",
"",
"module.exports = connection;",
],
"description": "Mongodb connection"
},
"nodeapp": {
"prefix": ["express"],
"body": [
"const express = require('express');",
"const bodyParser = require('body-parser');",
"",
"const app = express();",
"const PORT = 3000;",
"",
"app.use(bodyParser.json())",
"",
"app.get('/', (req, res) => {",
" res.status(200).json({ok: true})",
"});",
"",
"app.listen(PORT, () => console.log('App listening on PORT %s', PORT))",
],
"description": "Basic code to nodejs app with express and body-parser"
},
"express-async-get": {
"prefix": "express-async-get",
"body": [
"app.get('/', async (req, res) => {",
"",
" res.status(200).json([]);",
"});"
],
"description": ""
},
"express-async-get-by-id": {
"prefix": "express-async-get-by-id",
"body": [
"app.get('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});"
],
"description": ""
},
"express-async-post": {
"prefix": "express-async-post",
"body": [
"app.post('/', async (req, res) => {",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});"
],
"description": ""
},
"express-async-put": {
"prefix": "express-async-put",
"body": [
"app.put('/:id', async (req, res) => {",
" const { id } = req.params;",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});"
],
"description": ""
},
"express-async-delete": {
"prefix": "express-async-delete",
"body": [
"app.delete('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});"
],
"description": ""
},
"express-async-crud": {
"prefix": "express-async-crud",
"body": [
"app.get('/', async (req, res) => {",
"",
" res.status(200).json([]);",
"});",
"",
"app.get('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});",
"",
"app.post('/', async (req, res) => {",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});",
"",
"app.put('/:id', async (req, res) => {",
" const { id } = req.params;",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});",
"",
"app.delete('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});"
],
"description": ""
},
"express-controller": {
"prefix": "express-controller",
"body": [
"const { Router } = require('express');",
"",
"const router = Router();",
"",
"router.get('/', async (req, res) => {",
"",
" res.status(200).json([]);",
"});",
"",
"router.get('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});",
"",
"router.post('/', async (req, res) => {",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});",
"",
"router.put('/:id', async (req, res) => {",
" const { id } = req.params;",
" const { } = req.body;",
"",
" res.status(200).json({});",
"});",
"",
"router.delete('/:id', async (req, res) => {",
" const { id } = req.params;",
"",
" res.status(200).json({});",
"});",
"",
"module.exports = router;",
],
"description": ""
},
"arrow": {
"prefix": "arrow",
"body": [
"const f = (params) => {",
"",
"}"
]
},
"async-arrow": {
"prefix": "async-arrow",
"body": [
"const f = async (params) => {",
"",
"}"
]
},
"module-exports": {
"prefix": "moduleexport",
"body": [
"module.exports = {",
"",
"}"
]
},
"mongodb-query": {
"prefix": "mongodb-query",
"body": "await connection().then((db) => db.collection());"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment