This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const catSchema = mongoose.Schema({ | |
name: String | |
}); | |
catSchema.methods.speak = function () { | |
console.log('Meow', this.name); | |
}; | |
const Cat = mongoose.model('Cat', catSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// crea un schema | |
const catSchema = mongoose.Schema({ | |
name: String | |
}); | |
// crea un modelo basado en el schema | |
const Cat = mongoose.model('Cat', catSchema); | |
// creo una instancia del modelo | |
const garfield = new Cat({ name: 'Garfield' }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
const db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', () => { | |
console.log('connected'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id" : ObjectId("54c955492b7c8eb21818bd09"), | |
"address" : { | |
"street" : "2 Avenue", | |
"zipcode" : "10075", | |
"building" : "1480", | |
"coord" : [ -73.9557413, 40.7720266 ] | |
}, | |
"borough" : "Manhattan", | |
"cuisine" : "Italian", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html | |
head | |
title= title | |
body | |
h1= message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Asumiendo que tengo instalado el modulo pug => npm install pug --save | |
app.set('view engine', 'pug'); | |
app.get('/', (req, res) => { | |
res.render('index', { title: 'Hey', message: 'Hello there!' }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
var logger = (req, res, next) => { | |
console.log('LOGGED'); | |
next(); | |
} | |
app.use(logger); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use(express.static('public')) | |
app.use('img', express.static('images')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res.download(); // Envia un archivo para descargar | |
res.end(); // Finaliza el response | |
res.json(); // Envia json | |
res.jsonp(); // Envia jsonp | |
res.redirect(); // Redirecciona el request | |
res.render(); // Renderiza un template | |
res.send(); // Envia cualquier formato | |
res.sendFile(); // Envia archivos (stream) | |
res.sendStatus(); // Envia HTTP status code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/example', | |
(req, res, next) => { | |
console.log('Handler 1'); | |
next(); | |
}, (req, res) => { | |
res.send('Handler 2'); | |
}); |