diff --git a/examples/todos/.gitignore b/examples/todos/.gitignore new file mode 100644 index 00000000..adef95e2 --- /dev/null +++ b/examples/todos/.gitignore @@ -0,0 +1,3 @@ +log.txt +db +node_modules diff --git a/examples/todos/README.md b/examples/todos/README.md new file mode 100644 index 00000000..19d1e37a --- /dev/null +++ b/examples/todos/README.md @@ -0,0 +1,10 @@ +# express-pouchdb-server-boilerplate +Gets you set up with an express server with pouchdb, and admin user, and an app database. + +## Install +``` +npm install +node app.js +``` + +Then go to http://127.0.0.1:3030/index.html diff --git a/examples/todos/app.js b/examples/todos/app.js index 60fb7b05..a8e60a06 100644 --- a/examples/todos/app.js +++ b/examples/todos/app.js @@ -1,19 +1,38 @@ - -/** - * Module dependencies. - */ - -var express = require('express') - , path = require('path') - , morgan = require('morgan'); - +var http = require('axios'); +var read = require('read-yaml'); +var PouchDB = require('pouchdb'); +var express = require('express'); +var path = require('path'); var app = express(); +var config = read.sync('./config.yml'); +const DB_URL = `${config.protocol}${config.domain}:${config.port}${config.dbServerEndpoint}` +const DB_ADMIN_URL = `${config.protocol}${config.admin.username}:${config.admin.password}@${config.domain}:${config.port}${config.dbServerEndpoint}` + +app.use(config.dbServerEndpoint, require('express-pouchdb')(PouchDB.defaults({prefix: './db/'}))); +app.listen(config.port); -app.use(morgan('dev')); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/db', require('../../')); +async function setup() { + + // Set up the admin user. + try { + await http.put(`${DB_URL}/_config/admins/${config.admin.username}`, `"${config.admin.password}"`, {headers:{}}); + console.log("Admin created."); + } + catch (err) { + console.log("We already have admins."); + } + + // Set up the app database. + try { + await http.put(`${DB_ADMIN_URL}/todos`); + console.log("Todos database created."); + } + catch (err) { + console.log("We already have an app database."); + } -app.listen(3000); -console.log("Express server listening on port " + 3000); +} +setup(); diff --git a/examples/todos/config.yml b/examples/todos/config.yml new file mode 100644 index 00000000..dd47f45e --- /dev/null +++ b/examples/todos/config.yml @@ -0,0 +1,8 @@ +admin: + username: "admin" + password: "password" +domain: "127.0.0.1" +protocol: "http://" +port: 3030 +dbServerEndpoint: '/db' +appDatabase: 'app' diff --git a/examples/todos/package.json b/examples/todos/package.json new file mode 100644 index 00000000..db628262 --- /dev/null +++ b/examples/todos/package.json @@ -0,0 +1,24 @@ +{ + "name": "express-pouchdb-server-boilerplate", + "version": "0.0.0", + "description": "Boilerplate for creating an express-pouchdb-server", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "" + }, + "keywords": [ + "express-pouchdb", + "pouchdb" + ], + "author": "", + "license": "ISC", + "dependencies": { + "axios": "^0.16.2", + "express-pouchdb": "^2.3.7", + "read-yaml": "^1.1.0" + } +} diff --git a/examples/todos/public/scripts/app.js b/examples/todos/public/scripts/app.js index ba202611..99515d6e 100644 --- a/examples/todos/public/scripts/app.js +++ b/examples/todos/public/scripts/app.js @@ -9,7 +9,7 @@ // EDITING STARTS HERE (you dont need to edit anything above this line) var db = new PouchDB('todos'); - var remoteCouch = window.location.href + 'db/todos'; + var remoteCouch = window.location.origin + '/db/todos'; db.info(function(err, info) { db.changes({