Add structure for adding exported Ionic application to backend server
This commit is contained in:
40
app/IonicUnit.js
Normal file
40
app/IonicUnit.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const Unit = require('libflitter/Unit')
|
||||
const cors = require('cors')
|
||||
const Express = require('express')
|
||||
const path = require('path')
|
||||
|
||||
class IonicUnit extends Unit {
|
||||
static get services() {
|
||||
return [...super.services, 'configs', 'express', 'canon', 'utility']
|
||||
}
|
||||
|
||||
constructor(...args) {
|
||||
super(...args)
|
||||
|
||||
/**
|
||||
* Fully qualified path to the root of the ionic app.
|
||||
* @type {string}
|
||||
*/
|
||||
this.directory = path.resolve(path.dirname(this.utility.root()), this.configs.get('ionic.root'))
|
||||
}
|
||||
|
||||
async go(app) {
|
||||
app.express.use(cors())
|
||||
|
||||
app.express.use('/i', [
|
||||
this.canon.get('middleware::auth:UserOnly'),
|
||||
(req, res, next) => {
|
||||
const allowed_extensions = ['.html', '.js', '.css', '.svg', '.ttf', '.jpg', '.png', '.jpeg']
|
||||
for ( const k1 in allowed_extensions ) {
|
||||
if ( req.path.endsWith(allowed_extensions[k1]) ) return next()
|
||||
}
|
||||
|
||||
return res.sendFile(path.resolve(this.directory, 'index.html'))
|
||||
},
|
||||
Express.static(this.directory),
|
||||
])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = exports = IonicUnit
|
||||
Reference in New Issue
Block a user