diff --git a/bin/index.js b/bin/index.js index 8351cb1d5..e473f4f66 100755 --- a/bin/index.js +++ b/bin/index.js @@ -36,19 +36,19 @@ var argv = yargs function start(object, filename) { var port = process.env.PORT || argv.port var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host - + // Print http paths to console for (var prop in object) { console.log(chalk.grey(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop)) } - + // Print server URL prompt to console console.log( '\nYou can now go to ' + chalk.grey('http://' + hostname + ':' + port + '/\n') ) - + // Print 'create db snapshot' prompt to console console.log( 'Enter ' + chalk.cyan('`s`') + ' at any time to create a snapshot of the db\n' ) - + // Intitiate input steam and wait for user input; create db snapshot if 's' key is pressed process.stdin.resume() process.stdin.setEncoding('utf8') process.stdin.on('data', function (chunk) { @@ -58,7 +58,7 @@ function start(object, filename) { console.log('\nSaved snapshot to ' + chalk.cyan(file) + '\n') } }) - + // Initialise middleware/routing; listen for HTTP route requests if (filename) { var router = jsonServer.router(filename) } else { diff --git a/data/db.json b/data/db.json new file mode 100644 index 000000000..778ec7a82 --- /dev/null +++ b/data/db.json @@ -0,0 +1,16 @@ +{ + "posts": [ + { + "id": 1, + "title": "json-server", + "author": "typicode" + } + ], + "comments": [ + { + "id": 1, + "body": "some comment", + "postId": 1 + } + ] +} \ No newline at end of file diff --git a/data/users.htpasswd b/data/users.htpasswd new file mode 100644 index 000000000..a1002d0cb --- /dev/null +++ b/data/users.htpasswd @@ -0,0 +1,2 @@ +Peter David Carter:test +Aaron Gray:test diff --git a/package.json b/package.json index 8c49f342f..ae26a6240 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "0.6.6", + "version": "0.6.2", "description": "Serves JSON files through REST routes.", "main": "./src/index.js", "bin": "./bin/index.js", @@ -14,6 +14,7 @@ "errorhandler": "^1.2.0", "express": "^4.9.5", "got": "^1.2.2", + "http-auth": "^2.2.5", "lowdb": "^0.7.1", "method-override": "^2.1.2", "morgan": "^1.3.1", diff --git a/src/server.js b/src/server.js index 160a770f9..84b9054b7 100644 --- a/src/server.js +++ b/src/server.js @@ -3,19 +3,33 @@ var http = require('http') var express = require('express') var logger = require('morgan') var cors = require('cors') +var methodOverride = require('method-override') +var bodyParser = require('body-parser') var serveStatic = require('serve-static') var errorhandler = require('errorhandler') +// Authentication module. +var auth = require('http-auth'); +var basic = auth.basic({ + realm: "Private Area.", + file: __dirname + "/../data/users.htpasswd" +}); + + module.exports = function() { var server = express() - // Logger - server.use(logger('dev', { - skip: function(req, res) { return req.path === '/favicon.ico' } - })) + // Don't use logger if json-server is mounted + if (!module.parent) { + server.use(logger('dev')) + } - // Beautify JSON server.set('json spaces', 2) + server.use(bodyParser.json({limit: '10mb'})) + server.use(bodyParser.urlencoded({ extended: false })) + server.use(methodOverride()) + + server.use(auth.connect(basic)); // Serve static files if (fs.existsSync(process.cwd() + '/public')) { @@ -32,5 +46,5 @@ module.exports = function() { server.use(errorhandler()) } - return server -} \ No newline at end of file + return server +}