When running in non-local environments we recommend using a simple node script to start your deployd server. With each environment using its own script, you can easily separate out environmental variables (such as connection information) and actions (such as clearing out a test database).
// production.js
var deployd = require('deployd');
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
server.listen();
server.on('listening', function() {
console.log("Server is listening");
});
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
// staging.js
var deployd = require('deployd');
var server = deployd({
port: process.env.PORT || 5000,
env: 'staging',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
// remove all data in the 'todos' collection
var todos = server.createStore('todos');
todos.remove(function() {
// all todos removed
server.listen();
});
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
To run your app as a daemon, use the forever
module. You can install it from npm.
npm install forever -g
Then start the appropriate run script based on your environment.
forever start production.js
This will daemonize your app and make sure it runs even if it crashes.
To deploy your app on your server, or on a cloud hosting service such as EC2 or Heroku, the server must support Node.js.
Deployd also requires a MongoDB database, which can be hosted on the same server or externally.
If you have root shell access on the deployment server, you can install Deployd on it using the command npm install -g deployd
.
Otherwise, you will need to install Deployd as a dependency of your app itself using npm install deployd
in the root directory of your app.
You can use the dpd
CLI to run your server; this will start up an instance of MongoDB automatically, using the "data" folder. (Requires MongoDB installed on the server)
To set up the dashboard on your server, type dpd keygen
on your server's command line to create a remote access key. Type dpd showkey
to get the key; you should store this somewhere secure.
You can then go to the /dashboard
route on the server and type in that key to gain access.
Since Deployd is itself a node module, you can write your own scripts to run in production instead of using the command line interface. Read the Building a Custom Run Script Guide.
Note: Some hosts do not support WebSockets, so dpd.on()
may not work correctly on certain deployments.
Deployd is a node module and can be used inside other node programs or as the basis of an entire node program.
For an app in your current directory:
npm install deployd
You can also install globally:
npm install deployd -g
Here is a simple hello world using Deployd as a node module.
// hello.js
var deployd = require('deployd')
, options = {port: 3000};
var dpd = deployd(options);
dpd.listen();
Run this like any other node program.
node hello.js
db.connectionString
is set, the other db options are ignored.Note: If options.env is "development", the dashboard will not require authentication and configuration will not be cached. Make sure to change this to "production" or something similar when deploying.
process.server
. This means you can only run one Deployd server in a process.process.cwd
. Add this to ensure you are in the right directory: process.chdir(__dirname)
./dashboard
without a key you must run Deployd with the env
option set to development
.Deployd can be used with express/connect. Deployd will attach functions and handler to express server object.
Here is a simple hello world using Deployd as an express middleware.
// hello-server-attach.js
var PORT = process.env.PORT || 3000;
var ENV = process.env.NODE_ENV || 'development';
// setup http + express + socket.io
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, {'log level': 0});
// setup deployd
require('deployd').attach(server, {
socketIo: io, // if not provided, attach will create one for you.
env: ENV,
db: {host:'localhost', port:27017, name:'test-app'}
});
// After attach, express can use server.handleRequest as middleware
app.use(server.handleRequest);
// start server
server.listen(PORT);
Run this like any other express server.
node hello-server-attach.js
db.connectionString
is set, the other db options are ignored.Note: If options.env is "development", the dashboard will not require authentication and configuration will not be cached. Make sure to change this to "production" or something similar when deploying.
process.server
. This means you can only run one Deployd server in a process.process.cwd
. Add this to ensure you are in the right directory: process.chdir(__dirname)
./dashboard
without a key you must run Deployd with the env
option set to development
.By default, Deployd will load all npm modules found in node_modules
. This can be problematic if you want to use Grunt, gulp or other development tools: Deployd will try to load them since they are inside the node_modules
folder and will fail.
To avoid that, you can add a package.json and let Deployd know which dependencies to load.
Inside devDependencies
, you can add your grunt/gulp plugins and inside dependencies
, add the dpd modules you need.
Example:
"dependencies": {
"deployd": "^0.7.0",
"dpd-fileupload": "^0.0.10"
},
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jshint": "^1.6.1"
}
Let us know if you have any ideas to improve our docs. Open an issue on github, send us an email, or tweet us.
This entire site, including documentation written in markdown is available on github. Pull requests are appreciated!