├── config ├── controllers ├── api.js └── default.js ├── databases └── users.nosql ├── index.js ├── package.json ├── public └── favicon.ico ├── readme.md └── schemas └── users.js /config: -------------------------------------------------------------------------------- 1 | name : REST Service -------------------------------------------------------------------------------- /controllers/api.js: -------------------------------------------------------------------------------- 1 | exports.install = function() { 2 | 3 | // Sets cors for the entire API 4 | CORS(); 5 | 6 | ROUTE('GET /api/users/ *Users --> query'); 7 | ROUTE('GET /api/users/{id}/ *Users --> read'); 8 | ROUTE('POST /api/users/ *Users --> insert'); 9 | ROUTE('PUT /api/users/{id}/ *Users --> update'); 10 | ROUTE('DELETE /api/users/{id}/ *Users --> remove'); 11 | }; 12 | -------------------------------------------------------------------------------- /controllers/default.js: -------------------------------------------------------------------------------- 1 | exports.install = function() { 2 | ROUTE('GET /', plain_version); 3 | }; 4 | 5 | function plain_version() { 6 | var self = this; 7 | self.plain('REST Service {0}\nVersion: {1}'.format(CONF.name, CONF.version)); 8 | } -------------------------------------------------------------------------------- /databases/users.nosql: -------------------------------------------------------------------------------- 1 | {"id":"9pcc001ct41d","firstname":"Peter","lastname":"Sirka","search":"peter sirka","email":"petersirka@gmail.com","phone":"+421903163302","dtcreated":"2016-06-18T18:05:43.482Z"} 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // =================================================== 2 | // Total.js start script 3 | // https://www.totaljs.com 4 | // =================================================== 5 | 6 | const options = {}; 7 | 8 | // options.ip = '127.0.0.1'; 9 | // options.port = parseInt(process.argv[2]); 10 | // options.unixsocket = require('path').join(require('os').tmpdir(), 'app_name'); 11 | // options.unixsocket777 = true; 12 | // options.config = { name: 'Total.js' }; 13 | // options.sleep = 3000; 14 | // options.inspector = 9229; 15 | // options.watch = ['private']; 16 | // options.livereload = 'https://yourhostname'; 17 | // options.https = { key: Fs.readFileSync('keys/agent2-key.pem'), cert: Fs.readFileSync('keys/agent2-cert.pem')}; 18 | // options.watcher = true; // enables watcher for the release mode only controlled by the app `F.restart()` 19 | // options.edit = 'wss://www.yourcodeinstance.com/?id=projectname' 20 | 21 | // Service mode: 22 | options.servicemode = process.argv.indexOf('--servicemode', 1) !== -1; 23 | // options.servicemode = 'definitions,modules,config'; 24 | 25 | // Enables cluster: 26 | // options.cluster = 'auto'; 27 | // options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling) 28 | 29 | // Enables threads: 30 | // options.cluster = 'auto'; 31 | // options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling) 32 | // options.timeout = 5000; 33 | // options.threads = '/api/'; 34 | // options.logs = 'isolated'; 35 | 36 | var type = process.argv.indexOf('--release', 1) !== -1 ? 'release' : 'debug'; 37 | require('total4/' + type)(options); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "totalproject", 3 | "description": "Empty project", 4 | "version": "1.0.0", 5 | "main": "index.js", 6 | "dependencies": { 7 | "total4": "latest" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "keywords": ["empty", "project"], 13 | "author": "Peter Širka", 14 | "license": "MIT" 15 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/totaljs/emptyproject-restservice/0c4ebf846c19d769ef06e7dddca909529155bf79/public/favicon.ico -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Empty project: REST Service 2 | 3 | - [Documentation](https://docs.totaljs.com) 4 | - [Join Total.js Telegram](https://t.me/totaljs) 5 | - [Support](https://www.totaljs.com/support/) 6 | 7 | __Instructions__: 8 | 9 | - install the latest version of the __Total.js framework 4__ from NPM `$ npm install total4` 10 | - download example 11 | - run `$ node index.js` 12 | - open browser `http://127.0.0.1:8000` 13 | -------------------------------------------------------------------------------- /schemas/users.js: -------------------------------------------------------------------------------- 1 | NEWSCHEMA('Users', function(schema) { 2 | 3 | schema.define('firstname', 'Name(30)', true); 4 | schema.define('lastname', 'Name(30)', true); 5 | schema.define('email', 'Email', true); 6 | schema.define('phone', 'Phone'); 7 | 8 | schema.action('query', { 9 | name: 'Returns list of users', 10 | query: 'q:String', 11 | action: function($) { 12 | var builder = DB().find('nosql/users'); 13 | $.query.search && builder.search('search', $.query.search); 14 | builder.fields('id,firstname,lastname,dtcreated'); 15 | builder.sort('dtcreated_desc'); 16 | builder.callback($.callback); 17 | } 18 | }); 19 | 20 | schema.action('read', { 21 | name: 'Reads a user profile', 22 | params: 'id:UID', 23 | action: function($) { 24 | var params = $.params; 25 | DB().one('nosql/users').error('@(User not found)').where('id', params.id).callback($.callback); 26 | } 27 | }); 28 | 29 | schema.action('create', { 30 | name: 'Creates a new user profile', 31 | action: async function($, model) { 32 | 33 | model.id = UID(); 34 | model.dtcreated = NOW; 35 | model.search = (model.firstname + ' ' + model.lastname).toSearch(); 36 | 37 | // Inserts data 38 | await DB().insert('nosql/users', model).promise($); 39 | $.success(model.id); 40 | } 41 | }); 42 | 43 | schema.action('update', { 44 | name: 'Updates a user profile', 45 | params: 'id:UID', 46 | action: async function($, model) { 47 | var params = $.params; 48 | model.dtupdated = NOW; 49 | await DB.modify('nosql/users', model).id(params.id).error('@(User not found)').promise($); 50 | $.success(params.id); 51 | } 52 | }); 53 | 54 | schema.action('Remove', { 55 | name: 'Removes a user', 56 | params: 'id:UID', 57 | action: async function($) { 58 | var params = $.params; 59 | await DB().remove('nosql/users').id(params.id).error('@(User not found)').promise($); 60 | $.success(params.id); 61 | } 62 | }); 63 | }); --------------------------------------------------------------------------------