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 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4425b5d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js + +node_js: + - "8" + - "6" + +notifications: + email: + on_success: never + on_failure: always diff --git a/README.md b/README.md index c6fba08..d2457cc 100644 --- a/README.md +++ b/README.md @@ -1 +1,37 @@ -# 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). 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' }) + }) +})