Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example app and demonstrate some other best practices #239

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions examples/todos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
log.txt
db
node_modules
10 changes: 10 additions & 0 deletions examples/todos/README.md
Original file line number Diff line number Diff line change
@@ -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
45 changes: 32 additions & 13 deletions examples/todos/app.js
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.couchConfig.set('admins', config.admin.username, config.admin.password, function (err, previousValue) {})

might be more robust. Less chance, although that chance is already small admittedly, of someone hijacking your database before the admin is set. Note that I did not run the code, so using it would require testing it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less chance, although that chance is already small admittedly, of someone hijacking your database before the admin is set.

Lol, ya, I've thought about that but not overly worried. I didn't know about app.couchConfig.set. Cool stuff!

Copy link
Contributor

@QuentinRoy QuentinRoy Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.couchConfig.set does not seem to work. It makes it crash with "TypeError: refreshUsersDBImpl is not a function". What is weird is that it still set up the config file properly...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems what's happening in this case is that the users data base isn't ready yet, but setting up an admin will still cause it to try to update it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue here #341

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, if you store the PouchDB.defaults() intermediate result above, you could just do new PouchDB('todos') here. Again, it removes the HTTP roundtrip.

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();
8 changes: 8 additions & 0 deletions examples/todos/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
admin:
username: "admin"
password: "password"
domain: "127.0.0.1"
protocol: "http://"
port: 3030
dbServerEndpoint: '/db'
appDatabase: 'app'
24 changes: 24 additions & 0 deletions examples/todos/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion examples/todos/public/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down