{"id":20604798,"url":"https://github.com/themost-framework/express","last_synced_at":"2025-04-15T02:26:04.609Z","repository":{"id":33225326,"uuid":"151911226","full_name":"themost-framework/express","owner":"themost-framework","description":"MOST Web Framework Data ORM Express Middleware","archived":false,"fork":false,"pushed_at":"2025-04-05T08:17:37.000Z","size":2252,"stargazers_count":1,"open_issues_count":12,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T09:23:18.573Z","etag":null,"topics":["api","api-rest","data-model","express-middleware","odata","odata-server","odata-service","orm","orm-framework"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/themost-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-07T06:04:56.000Z","updated_at":"2025-04-05T08:17:41.000Z","dependencies_parsed_at":"2024-03-19T12:50:20.561Z","dependency_job_id":"4d2338ed-9d96-4251-838f-d32c55a4b95d","html_url":"https://github.com/themost-framework/express","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fexpress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fexpress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fexpress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fexpress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/themost-framework","download_url":"https://codeload.github.com/themost-framework/express/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248992939,"owners_count":21195113,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-rest","data-model","express-middleware","odata","odata-server","odata-service","orm","orm-framework"],"created_at":"2024-11-16T09:24:53.546Z","updated_at":"2025-04-15T02:26:04.596Z","avatar_url":"https://github.com/themost-framework.png","language":"JavaScript","readme":"[![npm](https://img.shields.io/npm/v/@themost%2Fexpress.svg)](https://www.npmjs.com/package/@themost%2Fexpress)\n![](https://github.com/themost-framework/express/workflows/test/badge.svg)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/15b61e7d887344358d3b9f15da638ba0)](https://www.codacy.com/gh/themost-framework/express/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=themost-framework/express\u0026amp;utm_campaign=Badge_Grade)\n![GitHub top language](https://img.shields.io/github/languages/top/themost-framework/express)\n[![License](https://img.shields.io/npm/l/@themost/express.svg)](/LICENSE)\n![GitHub last commit](https://img.shields.io/github/last-commit/themost-framework/express)\n![GitHub Release Date](https://img.shields.io/github/release-date/themost-framework/express)\n[![npm](https://img.shields.io/npm/dw/@themost/data)](https://www.npmjs.com/package/@themost%2Fexpress)\n\n## @themost/express\n@themost-framework middleware for express.js\n\n### Installation\n\n    npm i @themost/express\n\n### Generate new project\n\nInstall @themost/cli globally\n\n    npm i @themost/cli -g\n\nGenerate a new project by executing the following command:\n\n    themost new project my-app  --template express\n\nGo to project's directory:\n\n    cd my-app\n  \nInstall dependencies:\n\n    npm i\n  \nand serve the new application by executing:\n\n    npm run serve\n  \nOpen your browser and navigate to [http://127.0.0.1:3000](http://127.0.0.1:3000)\n    \n### Usage\n\nUse @themost/data application as an express middleware:\n\n    import express from 'express';\n    import path from 'path';\n    import {ExpressDataApplication, serviceRouter, dateReviver} from '@themost/express';\n    let app = express();\n    // data application setup\n    let dataApplication = new ExpressDataApplication(path.resolve(__dirname, 'config'));\n    // use @themost/express dateReviver helper function for parsing dates\n    app.use(express.json({\n      reviver: dateReviver \n    }));\n    // use data application middleware\n    app.use(dataApplication.middleware(app));\n    \nUse the service router for serving all the available data models:\n    \n    var serviceRouter = require('@themost/express').serviceRouter;\n    app.use('/api', passport.authenticate('bearer', { session: false }), serviceRouter);\n    \nor use the traditional way of serving data:\n\n    var peopleRouter = require('./routes/people');\n    app.use('/people', peopleRouter);\n\n    // # people.js\n    var express = require('express');\n    var router = express.Router();\n    \n    /* GET /people get persons listing. */\n    router.get('/', function(req, res, next) {\n      req.context.model('Person').filter(req.query).then(function(q) {\n          return q.getList().then(function(result) {\n              return res.json(result);\n          });\n      }).catch(function(err) {\n          return next(err);\n      });\n    });\n    \n    /* POST /people insert or update a person or an array of persons. */\n    router.post('/', function(req, res, next) {\n      if (typeof req.body === 'undefined') {\n        return res.status(400).send();\n      }\n      req.context.model('Person').save(req.body).then(function() {\n        return res.json(req.body);\n      }).catch(function(err) {\n          return next(err);\n      });\n    });\n    \n    /* GET /person/:id get a person by id. */\n    router.get('/:id', function(req, res, next) {\n      req.context.model('Person').where('id').equal(req.params.id).getItem().then(function(value) {\n        if (typeof value === 'undefined') {\n          return res.status(204).send();\n        }\n          return res.json(value);\n      }).catch(function(err) {\n          return next(err);\n      });\n    });\n    \n    /* DELETE /person/:id delete a person by id. */\n    router.delete('/:id', function(req, res, next) {\n      req.context.model('Person').where('id').equal(req.params.id).count().then(function(value) {\n        if (value === 0) {\n          return res.status(404).send();\n        }\n        // construct a native object\n        var obj = {\n          \"id\": req.params.id\n        };\n        //try to delete\n        return req.context.model('Person').remove(obj).then(function() {\n          return res.json(obj);\n        });\n      }).catch(function(err) {\n          return next(err);\n      });\n    });\n\n### Extend application container\n\nUse ExpressDataApplication#container to access and extend parent application. The following example represents an application service which extends container application router\n\n    # MyApplicationService.js\n \n    export class MyApplicationService extends ApplicationService {\n        constructor(app) {\n            super(app);\n            // subscribe for container\n            app.container.subscribe( container =\u003e {\n                if (container) {\n                    // create a router\n                    const newRouter = express.Router();\n                    newRouter.get('/message', (req, res) =\u003e {\n                        return res.json({\n                            message: 'Hello World'\n                        });\n                    });\n                    newRouter.get('/status', (req, res) =\u003e {\n                        return res.json({\n                            status: 'ok'\n                        });\n                    });\n                    // use router\n                    container.use('/a', newRouter);\n                }\n            });\n        }\n    }\n    \n    \n    # app.js\n    import {MyApplicationService} from './MyApplicationService';\n    ...\n    // use data application middleware\n    app.use(dataApplication.middleware(app));\n    // add application service\n    dataApplication.useService(MyApplicationService);\n    \n### Extend service router\n\n`@themost/express#serviceRouter` router may be extended to include extra service endpoints:\n\n    # ServiceRouterExtension.js\n \n    class ServiceRouterExtension extends ApplicationService {\n    constructor(app) {\n            super(app);\n            app.serviceRouter.subscribe( serviceRouter =\u003e {\n                // create new router\n                const addRouter = express.Router();\n                addRouter.get('/users/me/status', (req, res) =\u003e {\n                    return res.json({\n                        status: 'ok'\n                    });\n                });\n                // insert router at the beginning of serviceRouter.stack\n                serviceRouter.stack.unshift.apply(serviceRouter.stack, addRouter.stack);\n            });\n        }\n    }\n\n    # app.js\n\n    const app = express();\n    // create a new instance of data application\n    const application = new ExpressDataApplication(path.resolve(__dirname, 'test/config'));\n    // use extension\n    application.useService(ServiceRouterExtension);\n    app.use(express.json({\n        reviver: dateReviver\n    }));\n    // hold data application\n    app.set('ExpressDataApplication', application);\n    // use data middleware (register req.context)\n    app.use(application.middleware(app));\n    ...\n    // user service router\n    app.use('/api/', passport.authenticate('bearer', { session: false }), serviceRouter);\n\n### Use Response Formatters\n\nResponseFormatter service formats responses based on `Accept` request header.\n\nThe available response formatters are:\n\n- JsonResponseFormatter which is the default response formatter and returns JSON response.\n- XmlResponseFormatter which returns XML response.\n\nA response formatter may be added in ResponseFormatter#formatters collection:\n\n    // add application/json formatter\n    this.formatters.set('application/json', JsonResponseFormatter);\n    // add application/xml formatter\n    this.formatters.set('application/xml', JsonResponseFormatter);\n\nAn example of custom formatter:\n\n    class MyJsonFormatter extends HttpResponseFormatter {\n        \n        execute(req, res) {\n            return res.json(data);\n        }        \n    }\n\nNote: ResponseFormatter is not enabled by default and it should be registered after application initialization.\n\n    import express from 'express';\n    import path from 'path';\n    import {ExpressDataApplication, serviceRouter, dateReviver, ResponseFormatter} from '@themost/express';\n    let app = express();\n    // data application setup\n    let dataApplication = new ExpressDataApplication(path.resolve(__dirname, 'config')); \n    // initialize response formatter\n    dataApplication.useService(ResponseFormatter);\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fexpress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthemost-framework%2Fexpress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fexpress/lists"}