-
Notifications
You must be signed in to change notification settings - Fork 154
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
rjcorwin
wants to merge
9
commits into
pouchdb:master
Choose a base branch
from
rjcorwin:example-application-server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
631b3d7
Start adding application server example
rjcorwin 08cc22b
Fix use of Axios to send PUT of plain string body
rjcorwin cea0cdf
Add a static mounted public folder in example-application-server
rjcorwin 7150070
Add the Todos app to the application example server
rjcorwin c2e0080
Make the app database the todos database in the example
rjcorwin 2475656
Fix the remote database URL in the example app
rjcorwin 394e785
Update example app README
rjcorwin 944ad97
Merge application-server-boilerplate into todos example
rjcorwin c830a91
Update example readme
rjcorwin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
log.txt | ||
db | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, if you store the |
||
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, ya, I've thought about that but not overly worried. I didn't know about
app.couchConfig.set
. Cool stuff!There was a problem hiding this comment.
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...There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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