Skip to content

Instantly share code, notes, and snippets.

View crizstian's full-sized avatar

Cristian Ramirez crizstian

View GitHub Profile
@crizstian
crizstian / movie-service-server.js
Last active January 21, 2017 21:25
Example of express server app
'use strict'
const express = require('express')
const morgan = require('morgan')
const helmet = require('helmet')
const movieAPI = require('../api/movies')
const start = (options) => {
return new Promise((resolve, reject) => {
// we need to verify if we have a repository added and a server port
if (!options.repo) {
@crizstian
crizstian / movies-service-movies.js
Last active January 22, 2017 20:46
Example of movie service API
'use strict'
const status = require('http-status')
module.exports = (app, options) => {
const {repo} = options
// here we get all the movies
app.get('/movies', (req, res, next) => {
repo.getAllMovies().then(movies => {
res.status(status.OK).json(movies)
@crizstian
crizstian / mongodb-replicaset.js
Last active January 22, 2017 21:35
Example of how to connect to a mongodb 3.4 replica set from nodejs
const MongoClient = require('mongodb')
// here we create the url connection string that the driver needs
const getMongoURL = (options) => {
const url = options.servers
.reduce((prev, cur) => prev + `${cur.ip}:${cur.port},`, 'mongodb://')
return `${url.substr(0, url.length - 1)}/${options.db}`
}
// simple configuration file
// database parameters
const dbSettings = {
db: process.env.DB || 'movies',
user: process.env.DB_USER || 'cristian',
pass: process.env.DB_PASS || 'cristianPassword2017',
repl: process.env.DB_REPLS || 'rs1',
servers: (process.env.DB_SERVERS) ? process.env.DB_SERVERS.split(' ') : [
'192.168.99.100:27017',
@crizstian
crizstian / movie-service-index.js
Created January 22, 2017 23:17
Example for booting movie-service
'use strict'
// we load all the depencies we need
const {EventEmitter} = require('events')
const server = require('./server/server')
const repository = require('./repository/repository')
const config = require('./config/')
const mediator = new EventEmitter()
// verbose logging when we are starting the server
console.log('--- Movies Service ---')
@crizstian
crizstian / movie-service-repo.js
Last active January 23, 2017 01:38
Example of a repository
// repository.js
'use strict'
// factory function, that holds an open connection to the db,
// and exposes some functions for accessing the data.
const repository = (db) => {
// since this is the movies-service, we already know
// that we are going to query the `movies` collection
// in all of our functions.
const collection = db.collection('movies')
@crizstian
crizstian / movies.raml
Last active January 23, 2017 16:09
Movie API service example
#%RAML 1.0
title: cinema
version: v1
baseUri: /
types:
Movie:
properties:
id: string
title: string
@crizstian
crizstian / cinema-catalog-repo.js
Created January 29, 2017 20:03
Example of retrieving data from mongoDB
// more code above
const getCinemasByCity = (cityId) => {
return new Promise((resolve, reject) => {
const cinemas = []
const query = {city_id: cityId}
const projection = {_id: 1, name: 1}
// example of making a find query to mongoDB,
// passign a query and projection objects.
const cursor = db.collection('cinemas').find(query, projection)
@crizstian
crizstian / cinema-catalog-service-api.js
Created January 29, 2017 20:16
Example of express routes
'use strict'
const status = require('http-status')
module.exports = (app, options) => {
const {repo} = options
app.get('/cinemas', (req, res, next) => {
repo.getCinemasByCity(req.query.cityId)
.then(cinemas => {
res.status(status.OK).json(cinemas)
@crizstian
crizstian / cinema-catalog-api-test.js
Created January 29, 2017 20:23
Example of mocha testing
/* eslint-env mocha */
const request = require('supertest')
const server = require('../server/server')
process.env.NODE = 'test'
describe('Movies API', () => {
let app = null
const testCinemasCity = [{
'_id': '588ac3a02d029a6d15d0b5c4',
'name': 'Plaza Morelia'