Created
May 8, 2017 14:48
-
-
Save novohispano/acf33ac03696a5491d8ec12b19457487 to your computer and use it in GitHub Desktop.
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 environment = process.env.NODE_ENV || 'development' | |
const configuration = require('./knexfile')[environment] | |
const database = require('knex')(configuration) | |
database.raw( | |
'INSERT INTO secrets (message, created_at) VALUES (?, ?)', | |
["I open bananas from the wrong side", new Date] | |
).then(() => { | |
database.raw('SELECT * FROM secrets WHERE id = 1 LIMIT 1') | |
.then((data) => { | |
console.log(data.rows) | |
process.exit() | |
}) | |
}) |
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
exports.up = function(knex, Promise) { | |
let createQuery = `CREATE TABLE secrets( | |
id SERIAL PRIMARY KEY NOT NULL, | |
message TEXT, | |
created_at TIMESTAMP | |
)` | |
return knex.raw(createQuery) | |
}; | |
exports.down = function(knex, Promise) { | |
let dropQuery = `DROP TABLE secrets` | |
return knex.raw(dropQuery) | |
}; |
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
// Update with your config settings. | |
module.exports = { | |
development: { | |
client: 'pg', | |
connection: 'postgresql://localhost/secrets', | |
migrations: { | |
directory: './db/migrations' | |
}, | |
seeds: { | |
directory: './db/seeds/dev' | |
}, | |
useNullAsDefault: true | |
}, | |
test: { | |
client: 'pg', | |
connection: 'postgresql://localhost/secrets', | |
migrations: { | |
directory: './db/migrations' | |
}, | |
seeds: { | |
directory: './db/seeds/test' | |
}, | |
useNullAsDefault: true | |
}, | |
production: { | |
client: 'pg', | |
connection: process.env.DATABASE_URL, | |
migrations: { | |
directory: './db/migrations' | |
}, | |
seeds: { | |
directory: './db/seeds/production' | |
}, | |
useNullAsDefault: true | |
}, | |
} |
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
{ | |
"name": "secret-box", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "NODE_ENV=test mocha test/*-test.js", | |
"start": "nodemon server.js", | |
"debug": "NODE_ENV=test mocha debug test/*-test.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.17.1", | |
"express": "^4.15.2", | |
"knex": "^0.13.0", | |
"md5": "^2.2.1", | |
"pg": "^6.1.5" | |
}, | |
"devDependencies": { | |
"chai": "^3.5.0", | |
"mocha": "^3.3.0", | |
"nodemon": "^1.11.0", | |
"request": "^2.81.0" | |
} | |
} |
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
web: node server.js |
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 Secret = require('../models/secret') | |
function show (request, response) { | |
Secret.find(request.params.id).then((data) => { | |
if (data == null) { | |
response.sendStatus(404) | |
} else { | |
response.json(data) | |
} | |
}) | |
} | |
function create (request, response) { | |
const message = request.body.message | |
if (!message) { | |
response.status(422).send({ | |
error: 'No message property provided' | |
}) | |
} else { | |
Secret.create(message).then((data) => { | |
response.status(201).json(data) | |
}) | |
} | |
} | |
module.exports = { | |
show: show, | |
create: create | |
} |
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
exports.seed = function(knex, Promise) { | |
return knex.raw('TRUNCATE secrets RESTART IDENTITY') | |
.then(() => { | |
return Promise.all([ | |
knex.raw('INSERT INTO secrets (message, created_at) VALUES (?, ?)', ["I hate mashed potatoes", new Date]), | |
knex.raw('INSERT INTO secrets (message, created_at) VALUES (?, ?)', ["I love rap music", new Date]), | |
knex.raw('INSERT INTO secrets (message, created_at) VALUES (?, ?)', ["I hate game shows", new Date]) | |
]) | |
}) | |
}; |
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 assert = require('chai').assert | |
const app = require('../server') | |
const request = require('request') | |
const Secret = require('../lib/models/secret') | |
describe('Server', () => { | |
before(done => { | |
this.port = 9876 | |
this.server = app.listen(this.port, (err, result) => { | |
if (err) { return done(err) } | |
done() | |
}) | |
this.request = request.defaults({ | |
baseUrl: 'http://localhost:9876/' | |
}) | |
}) | |
after(() => { | |
this.server.close() | |
}) | |
it('should exist', () => assert(app) ) | |
describe('GET /', () => { | |
it('should return a 200', (done) => { | |
this.request.get('/', (error, response) => { | |
if (error) { done(error) } | |
assert.equal(response.statusCode, 200) | |
done() | |
}) | |
}) | |
it('should have a body with the name of the application', (done) => { | |
var title = app.locals.title | |
this.request.get('/', (error, response) => { | |
if (error) { done(error) } | |
assert(response.body.includes(title), `"${response.body}" does not include "${title}".`) | |
done() | |
}) | |
}) | |
}) | |
describe('GET /api/secrets/:id', () => { | |
beforeEach((done) => { | |
Secret.create('I open bananas from the wrong side') | |
.then(() => done()) | |
}) | |
afterEach((done) => { | |
Secret.destroyAll() | |
.then(() => done()) | |
}) | |
it('should return a 404 if the resource is not found', (done) => { | |
this.request.get('/api/secrets/1000', (error, response) => { | |
if (error) { done(error) } | |
assert.equal(response.statusCode, 404) | |
done() | |
}) | |
}) | |
it('should have the id and the message from the resource', (done) => { | |
this.request.get('/api/secrets/1', (error, response) => { | |
if (error) { done(error) } | |
const id = 1 | |
const message = "I open bananas from the wrong side" | |
let parsedSecret = JSON.parse(response.body) | |
assert.equal(parsedSecret.id, id) | |
assert.equal(parsedSecret.message, message) | |
assert.ok(parsedSecret.created_at) | |
done() | |
}) | |
}) | |
}) | |
describe('POST /api/secrets', () => { | |
it('should not return a 404', (done) => { | |
this.request.post('/api/secrets', (error, response) => { | |
if (error) { done(error) } | |
assert.notEqual(response.statusCode, 404) | |
done() | |
}) | |
}) | |
it('should receive and store data', (done) => { | |
const message = { | |
message: 'I like pineapples!' | |
} | |
this.request.post('/api/secrets', { form: message }, (error, response) => { | |
if (error) { done(error) } | |
const id = 1 | |
const message = "I like pineapples!" | |
let parsedSecret = JSON.parse(response.body) | |
assert.equal(parsedSecret.id, id) | |
assert.equal(parsedSecret.message, message) | |
assert.ok(parsedSecret.created_at) | |
done() | |
}) | |
}) | |
}) | |
}) |
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() | |
const bodyParser = require('body-parser') | |
const SecretsController = require('./lib/controllers/secrets-controller') | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.set('port', process.env.PORT || 3000) | |
app.locals.title = 'Secret Box' | |
app.get('/', (request, response) => { | |
response.send(app.locals.title) | |
}) | |
app.get('/api/secrets/:id', SecretsController.show) | |
app.post('/api/secrets', SecretsController.create) | |
if (!module.parent) { | |
app.listen(app.get('port'), () => { | |
console.log(`${app.locals.title} is running on ${app.get('port')}.`) | |
}) | |
} | |
module.exports = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment