-
Notifications
You must be signed in to change notification settings - Fork 13
Build your Classic App tutorial
Building your own app for Cozy Light is almost like building a Node.js app based on Express. It just requires one function in your main file. That functions receive port and host on which to listen in paremeters. It should notify that server is started by sending back the app
and server
variables to the call function.
var express = require('express');
module.exports.start = function (options, callback) {
var port = options.port || process.env.PORT || 9104;
var host = options.host || process.env.HOST || '127.0.0.1';
var app = express();
app.get('/', function (req, res) { res.send('Hello world!'); });
var server = app.listen(port, host, function () {
callback(null, app, server);
});
}
This is how should look your main file. As you understand, once you're done with the start function, you're done with the Express app writing.
What is cool with Cozy Light, is that every apps share the same database. That way, they can communicate each other. The database is given trough the options of the start function. It's a PouchDB database instance.
var express = require('express');
module.exports.start = function (options, callback) {
var port = options.port || process.env.PORT || 9104;
var host = options.host || process.env.HOST || '127.0.0.1';
var db = options.db;
var app = express();
app.get('/', function (req, res) { res.send('Hello world!'); });
app.get('/data/:id', function (req, res, next) {
db.get(req.params.id, function (err, doc) {
if (err) {
next(err);
} else {
res.send(doc);
}
});
});
app.post('/data', function (req, res, next) {
db.post(req.body, function (err, doc) {
if (err) {
next(err);
} else {
res.status(201).send(doc);
}
});
});
var server = app.listen(port, host, function () {
callback(null, app, server);
});
}
If you want to build very small apps previous options are enough. But if you want to build a full featured single-page app we recommend you to use our canvas based on Backbone.js for the frontend and Americano a thrin wrapper around Express to make the Cozy app development easier.
Once your app properly coded add a proper NPM manifest:
{
"name": "cozy-light-template",
"displayName": "Template",
"type": "classic",
"version": "1.0.0",
"description": "A template project for Cozy applications written in JavaScript",
"engines": [
"node = 0.10.x"
],
"bin": {
"template": "bin/template"
},
"main": "server.js",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/cozy-labs/cozy-light-template.git"
},
"main": "server.js",
"dependencies": {
"americano": "0.3.11",
"americano-cozy-pouchdb": "0.3.18",
"path-extra": "^0.3.0"
},
"devDependencies": { },
"scripts": {
"start": "node server.js"
}
}
If you want additional details, ask to the community : https://forum.cozy.io