From 1ef5e99391b254f606af7af48ba3931bd75b7dc0 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 15 Sep 2017 15:00:05 +0200 Subject: [PATCH 1/6] Updated .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 00cbbdf..ad7dae2 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,8 @@ typings/ # dotenv environment variables file .env +# mac files +.DS_Store + +# vim swap files +*.swp From 96f95dd638b61a72a5a848899ea3715b4c409bf3 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 15 Sep 2017 15:00:14 +0200 Subject: [PATCH 2/6] Added travis --- .travis.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..da06525 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: node_js + +node_js: + - "8" + - "7" + - "6" + - "4" + +notifications: + email: + on_success: never + on_failure: always From 8546fe64b4239c5d0a2806b9b7f0af5c076b4df3 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 15 Sep 2017 15:00:25 +0200 Subject: [PATCH 3/6] Updated README.md --- README.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6fba08..4d32ac4 100644 --- a/README.md +++ b/README.md @@ -1 +1,36 @@ -# example \ No newline at end of file +
+ +
+ +
+[![Build Status](https://travis-ci.org/fastify/example.svg)](https://travis-ci.org/fastify/example) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/fastify) +
+
+ +- *How can I structure my Fastify application?* +- *How can I use plugins?* +- *How do I write my test?* + +If you are asking yourself these questions you are in the right place! + +## Usage +``` +# Clone the project +git clone https://github.com/fastify/example.git + +# Install the dependencies +npm i + +# Run the server! +npm start +``` + +## Acknowledgements + +This project is kindly sponsored by: +- [nearForm](http://nearform.com) +- [LetzDoIt](http://www.letzdoitapp.com/) + +## License + +Licensed under [MIT](./LICENSE). From 86b4d43b17b374ba4ade0d9d21e9143bd40dcc41 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 15 Sep 2017 15:00:35 +0200 Subject: [PATCH 4/6] First code commit --- package.json | 30 ++++++++++++++++++++++++++++++ server.js | 41 +++++++++++++++++++++++++++++++++++++++++ test/basic.test.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 package.json create mode 100644 server.js create mode 100644 test/basic.test.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..413b17d --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "example", + "version": "0.1.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "standard && tap test/*.test.js", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/fastify/example.git" + }, + "keywords": [], + "author": "Tomas Della Vedova - @delvedor (http://delved.org)", + "license": "MIT", + "bugs": { + "url": "https://github.com/fastify/example/issues" + }, + "homepage": "https://github.com/fastify/example#readme", + "dependencies": { + "fastify": "^0.27.0", + "minimist": "^1.2.0" + }, + "devDependencies": { + "shot": "^3.4.2", + "standard": "^10.0.3", + "tap": "^10.7.2" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..8482f23 --- /dev/null +++ b/server.js @@ -0,0 +1,41 @@ +'use strict' + +const minimist = require('minimist') +const Fastify = require('fastify') + +function build (opts, callback) { + const fastify = Fastify({ + level: opts['log-level'] || (process.env.NODE_ENV === 'production' ? 'error' : 'info') + }) + + fastify.get('/status', (req, reply) => { + reply.send({ status: 'ok' }) + }) + + fastify.ready(err => { + callback(err, fastify, opts) + }) +} + +function onReady (err, instance, opts) { + if (err) throw err + instance.listen(opts.port, err => { + if (err) throw err + console.log(`server listening on ${instance.server.address().port}`) + }) +} + +if (require.main === module) { + build(minimist(process.argv.slice(2), { + integer: ['port'], + alias: { + port: 'p', + 'log-level': 'L' + }, + default: { + port: 3000 + } + }), onReady) +} + +module.exports = { build } diff --git a/test/basic.test.js b/test/basic.test.js new file mode 100644 index 0000000..11e7a25 --- /dev/null +++ b/test/basic.test.js @@ -0,0 +1,31 @@ +'use strict' + +const { test, beforeEach, afterEach } = require('tap') +const server = require('../server') +var fastify = null + +beforeEach(done => { + server.build({ + 'log-level': 'silent' + }, (err, instance) => { + if (err) throw err + fastify = instance + done() + }) +}) + +afterEach(done => { + fastify.close(done) +}) + +test('The server should start', t => { + t.plan(2) + + fastify.inject({ + method: 'GET', + url: '/status' + }, res => { + t.equal(res.statusCode, 200) + t.deepEqual(JSON.parse(res.payload), { status: 'ok' }) + }) +}) From 0b4729046d97205d0faf5f7f82ab1bbbec69c714 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 15 Sep 2017 15:04:42 +0200 Subject: [PATCH 5/6] Test only Node v6 and v8 --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index da06525..4425b5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,7 @@ language: node_js node_js: - "8" - - "7" - "6" - - "4" notifications: email: From 0c38d15b8f04750f7b22a96dc72e7a5b6101d254 Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Fri, 15 Sep 2017 21:36:33 +0200 Subject: [PATCH 6/6] Updated README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4d32ac4..d2457cc 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@
+ [![Build Status](https://travis-ci.org/fastify/example.svg)](https://travis-ci.org/fastify/example) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/fastify)