From edf553c0ebdacb2261f12cdb290e679b56c501dd Mon Sep 17 00:00:00 2001 From: Typicode Date: Wed, 19 Mar 2014 15:13:36 +0100 Subject: [PATCH] Going back to express --- package.json | 10 ++---- public/favicon.ico | Bin 0 -> 318 bytes public/images/logo.png | Bin 2723 -> 0 bytes public/index.html | 44 ++++++++++++----------- public/stylesheets/style.css | 47 +++++-------------------- src/command.js | 38 ++++++++++---------- src/server.js | 66 ++++++++++++++--------------------- 7 files changed, 81 insertions(+), 124 deletions(-) create mode 100644 public/favicon.ico delete mode 100644 public/images/logo.png diff --git a/package.json b/package.json index 21f111f..effa268 100644 --- a/package.json +++ b/package.json @@ -8,18 +8,14 @@ "test": "test" }, "dependencies": { - "commander": "~2.0.0", "cors": "~2.1.0", - "express": "~3.4.4", - "logan": "~0.0.2", + "express": "^3.4.8", "superagent": "~0.15.7", "underscore": "~1.5.2", "underscore.inflections": "~0.2.1", - "moment": "~2.4.0", - "low": "^0.4.2", - "restify": "^2.6.3", + "low": "^0.5.0", "yargs": "^1.2.1", - "ecstatic": "^0.4.13" + "chalk": "^0.4.0" }, "devDependencies": { "supertest": "~0.8.1", diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..bfdd40dc7202fd8bcd83e37b2d3cd8036e947b6e GIT binary patch literal 318 zcmZQzU<5(|0RbS%!l1#(z#zuJz@P!d0zj+)#2|4HXaJKC0wgya!2kaY4FBpG7!Ecw bFzo-!zyRWd@b~wq7$y!<15yjp0}=xOPhK7B literal 0 HcmV?d00001 diff --git a/public/images/logo.png b/public/images/logo.png deleted file mode 100644 index 8fd3cc15cf312dc3e8b13c5af161ba64a7c2579e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2723 zcmV;U3S9MxP)xN3WG*sy2qIX&MyTYEz}|s8q&i$I5!B zRn=|k7^9`-rjR8e1PJ5?a*@PwY$x6yFCjm2IM-|YP4fFnmMtTG`T6<0=kq?l=P~&G zem}aDlX)cEW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(POAVkwVnbLQu$3AD1Eg7QxlWb`_VU|^0Fg7b_w-&M!QCSSI zd1~MP-Y1x8QSfY<D?;Z5#ya0!%p zvB?KnUx+HiK+qW!5M#A?Uup&;(XBjq; z02X%p!&Sq9n15UEW6^iO&UpEQkG9;fx#Vee%=74|00tlR2<*fE54-RoG`^5$q~e zX!&G>4cYKkw_eh473Q9X3koH~+ngaP{|(}&4P3u=LT1@NW+_Tgw_fm%=& zj)Px02ie00TMFx%2XenwsD_LImQ00pH}?c>F4%fWe#Y0KYI+nLz85A$1Q`qnD0xSP zPfb_g{mP-7p@oJShuR80ozoSWuB)fw%)&-Pjn^CU$;-?b_;^lNfSRr@#ao1JW)5RB zHS7YVXPRL7@zGNjez=lVpj1jp-_ll5eo-&!E3c%lltbk7NcbX01Qm=`<;Dve$ch1% zcDg^&3I}uGg>?1Y_`%-MZ<#87`(T&EK_AMx>yvBn!!sgt;AmE&1$NJZhb9C8crd_# zaD4tKU)1Cpjaoh){&4re9Fj_b>D1q<;e|q|93`u^R2#gSIWX6~(qKU3s@Zy8)THtG zML2tsI7kYqK&1_x})2z*9AqQP{k+zmS~iSrVd zqDkzcOd=R>W4b7h81mI#IKXtO&I|8UiXy0AHK+k(LA;11UNgD(2+1)4Myqozu(uNa zULy(+gi?1!i)VwLNa-1MKjy(`Rn;a65PV(>2WsI`tK92Pje@)c@!V-%!tGCTDYJ1^ z5lna0dqs~bY7y_nEl}MCE$t#BUW2zQOJTH0cz0t=5N(D@5yFS%w2Noxopw=rUU`qg dNB}by^?%KDnDJJmj1>R?002ovPDHLkV1o6|4$}Yt diff --git a/public/index.html b/public/index.html index 8f8b172..025f97f 100644 --- a/public/index.html +++ b/public/index.html @@ -1,28 +1,37 @@ JSON Server - +
- - -

- Congrats! You're successfully running JSON Server. +

+


- -

Resources

+ +

+ + Congrats! You're successfully running JSON Server. + +

+ +
+ +

Routes

+

+ Here are the resources that JSON Server has loaded: +

    loading, please wait...

- To view database current state: + You can view database current state at any time:

  • db @@ -30,26 +39,19 @@

-

Requests

- Resources can be accessed in various ways. -

-

- JSON Server supports: -

    -
  • GET, POST, PUT, PATCH, DESTROY and OPTIONS verbs.
  • -
  • JSONP or CORS cross domain requests.
  • -
+ You can use GET, POST, PUT... and access your resources from anywhere + using CORS and JSONP.

-

Documentation

+

Documentation

View README on GitHub.

-

Issues

+

Issues

Please go here.

diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 1464e6c..e46e5a1 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,43 +1,12 @@ -body { - padding: 50px; - font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +img { + padding-top: 50px; + padding-bottom: 20px; } -h1 { - font-weight: lighter; +li { + list-style-type: square; } -a { - /*color: #00B7FF;*/ - color: #3D72D1; -} - -hr { - border: 1px #EEE solid; -} - -a.logo { - display: block; - text-align: center; - margin-bottom: 50px; -} - -.alert-success { - font-size: 18px; -} - -/*.logo { - background: #00B7FF; - color: white; - font-size: 48px; - width: 100px; - height: 100px; - text-align: center; - vertical-align: middle; - line-height: 90px; - border-radius: 100px; - transform:rotate(90deg); - -ms-transform:rotate(90deg); - -webkit-transform:rotate(90deg); - margin-bottom: 50px; -}*/ \ No newline at end of file +h4 { + padding-top: 20px; +} \ No newline at end of file diff --git a/src/command.js b/src/command.js index b61e0cf..25d8573 100644 --- a/src/command.js +++ b/src/command.js @@ -1,40 +1,40 @@ -var yargv = require('yargs') + +var chalk = require('chalk') var request = require('request') var low = require('low') var server = require('./server') -function show() { - for (var prop in low.db) { - console.log(server.url + '/' + prop) - } +function hello() { + console.log( + chalk.green('\n{^ ^} Heya!\n') + ) } function start(port) { - server.listen(port, function() { - console.log('%s listening at %s', server.name, server.url); - }) + for (var prop in low.db) { + console.log('http://localhost:' + port + '/' + chalk.green(prop)) + } + + server.listen(port) } -function run() { - var argv = yargv.usage('Usage: $0 ') - .demand(1) - .default('port', 3000) - .argv +function run(argv) { + hello(); var source = argv._[0] + console.log('Loading database from ' + source + '\n') + if (/\.json$/.test(source)) { var path = process.cwd() + '/' + source low.path = path low.db = require(path); - show() - start() + start(argv.port) } if (/\.js$/.test(source)) { var path = process.cwd() + '/' + source low.db = require(path).run(); - show() start(argv.port) } @@ -47,10 +47,12 @@ function run() { low.db = JSON.parse(res.text) } }) - show() - start() + start(argv.port) } + console.log( + '\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n' + ) process.stdin.resume() process.stdin.setEncoding('utf8') diff --git a/src/server.js b/src/server.js index f8b454f..530900f 100644 --- a/src/server.js +++ b/src/server.js @@ -1,33 +1,31 @@ -var _ = require('underscore') -var low = require('low') -var ecstatic = require('ecstatic') -var restify = require('restify') -var utils = require('./utils') +var express = require('express') +var http = require('http') +var path = require('path') +var _ = require('underscore') +var low = require('low') +var utils = require('./utils') low._.createId = utils.createId -var server = restify.createServer({ - name: 'JSON Server' -}) +var server = express() -server.use(restify.acceptParser(server.acceptable)) -server.use(restify.queryParser()) -server.use(restify.bodyParser()) -server.use(restify.CORS()) -server.use(restify.jsonp()) -server.use(restify.gzipResponse()) +server.set('port', process.env.PORT || 3000) +server.use(express.logger('dev')) +server.use(express.json()) +server.use(express.urlencoded()) +server.use(express.methodOverride()) +server.use(express.static(path.join(__dirname, '../public'))) +server.use(server.router) + +if ('development' == server.get('env')) { + server.use(express.errorHandler()); +} routes = {} -server.get(/^\/$|.*(.css)/, restify.serveStatic({ - 'directory': __dirname + '/../public', - 'default': 'index.html' -})) - // GET /db routes.db = function(req, res, next) { - res.send(low.db) - next() + res.jsonp(low.db) } // GET /:resource?attr=&attr= @@ -46,8 +44,7 @@ routes.list = function(req, res, next) { query = low(req.params.resource).where(properties) } - res.send(query.value()) - next() + res.jsonp(query.value()) } // GET /:parent/:parentId/:resource @@ -63,8 +60,7 @@ routes.nestedList = function(req, res, next) { .where(properties) .value() - res.send(resource) - next() + res.jsonp(resource) } // GET /:resource/:id @@ -73,8 +69,7 @@ routes.show = function(req, res, next) { .get(+req.params.id) .value() - res.send(resource) - next() + res.jsonp(resource) } // POST /:resource @@ -83,8 +78,7 @@ routes.create = function(req, res, next) { .insert(req.body) .value() - res.send(resource) - next() + res.jsonp(resource) } // PUT /:resource/:id @@ -94,21 +88,15 @@ routes.update = function(req, res, next) { .update(+req.params.id, req.body) .value() - res.send(resource) - next() + res.jsonp(resource) } // DELETE /:resource/:id routes.destroy = function(req, res, next) { - try { - low(req.params.resource).remove(+req.params.id) - utils.clean() + low(req.params.resource).remove(+req.params.id) + utils.clean() - res.send(204) - next() - } catch(e) { - console.trace(e) - } + res.send(204) } server.get('/db', routes.db)