express-e2e
v1.2.15
Published
End to end nodejs framework with ExpressJS, Micro services, web socket, config encryption and much more
Downloads
61
Readme
express-e2e framework
Start the express app in few lines of code. Forget about other dependencies.
More customizable/configurable Advanced End to end nodejs framework with ExpressJS, MongoDB, Security, Micro services, web socket, advanced config encryption, CQRS and much more through the settings.json configuration.
More documentation to come through about advanced features. Please keep an eye on here.
Installation
npm install express-e2e --save
What's New
Version 1.2.2
Usage
Start the app in default port 3000 with index.html
,
// server.js
const e2eApp = require('express-e2e');
// Configuration
const settings = {
spaHtml: "./app/views/index.html" // relative path to index.html
};
// Init the application
e2eApp.init(settings, (app) => {
// Do nothing
});
Start the app,
node server.js
Wait for the console message something like AppName V1.0.0 started on port 3000.
and then browse the index.html
on http://localhost:3000.
Configure PORT
Start the app with custom port,
// server.js
const e2eApp = require('express-e2e');
// Configuration
const settings = {
port: 5000, // This can be overridden by "process.env.PORT"
spaHtml: "./app/views/index.html" // relative path to index.html
};
// Init the application
e2eApp.init(settings, (app) => {
// Do nothing
});
Static Directory
To setup a static directory to serve *.js
, *.css
, images
and other static files.
// Configuration
const settings = {
staticDir: "./public"
};
favicon.ico
To process the browser request /favicon.ico
, set the favIcon
property as below with the relative path of .ico
or .png
or .jpg
of the favicon image in the settings.json.
// Configuration
const settings = {
favIcon: "./public/images/favicon.png"
};
View Engine
To set up a view/template engine such as jade
, ejs
, haml
, swig
etc to serve the web pages instead of index.html
.
// Configuration
const settings = {
viewEngine: "jade",
viewsExt: "jade", // views file extension such as index.jade
viewsDir: "./app/facade/ui/views" // views directory (relative path)
};
then install the respective view/template engine module,
npm install jade --save
SPA Route
For Single Page APP (SPA), set the controller/presenter of the index.jade
as below.
// Configuration
const settings = {
html5RoutingPresenter: "./app/facade/ui/controllers/core.controller"
};
i) To serve view/template engine pages
// ./app/facade/ui/controllers/core.controller.js
exports.index = (req, res, next) => {
// To use with the view engine. This will serve the index.jade etc
res.render('index', {});
};
ii) OR, to serve plain html
page without any view/template engine setup
// ./app/facade/ui/controllers/core.controller.js
const path = require('path');
exports.index = (req, res, next) => {
// To send index.html without any view engine setup
res.sendFile(path.resolve('./app/views/index.html'));
};
Express Routes
To initiate other view/api route files, set the routeFiles
property with the pattern of routes directory and files as below.
// Configuration
const settings = {
routeFiles: "./app/facade/**/routes/**/*.js"
};
Sample route file,
// ./app/facade/api/routes/user.js
const userController = require('../controllers/user');
module.exports = (app) => {
app.route('/api/user').get(userController.get);
};
Configure mongoose connection
To bootstrap mongoose connection, set the mongodb connection string as below.
// Configuration
const settings = {
mongoUrl: "mongodb://localhost/databaseName" // This can be overridden by "process.env.MONGO_URL"
};
Note: In case of failure in the db connection, it is set to be retried with a default of 5 maximum attempts.
To turn this off or change the retry count, set the maxDbConnectionRetry
property in the settings.json.
// Configuration
const settings = {
maxDbConnectionRetry: 1, // 1 to turn off the retry or more than 1 to change the retry count
mongoUrl: "mongodb://localhost/databaseName" // This can be overridden by "process.env.MONGO_URL"
};
mongoose models
To initiate all the mongoose models, set the modelFiles
property with the pattern of models directory and files as below.
// Configuration
const settings = {
modelFiles: "./app/models/**/*.js"
};
Sample model file,
// ./app/models/user.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: ''
},
lastName: {
type: String,
trim: true,
default: ''
},
username: {
type: String,
trim: true
}
});
mongoose.model('User', UserSchema);
Security
Following security headers are set automatically using helmet module. More to come through.
- X-Frame-Options: Deny (to prevent clickjacking)
- X-XSS-Protection: 1; mode=block (XSS protections)
- X-Content-Type-Options: nosniff (to prevent mime sniffing)
- X-Download-Options: noopen (sets X-Download-Options for IE8+)
Schedule Job
To schedule a nodejs job to run at specific interval, day or week. The job can be useful to check something in the db, notify users via email or update something in the database etc at regular interval/time.
Set cronJobs
property with the jobs file directory as below.
// Configuration
const settings = {
cronJobs: "./app/jobs/**/*.job.js"
};
Sample job
// ./app/jobs/log.job.js
const MongooseRepository = require('mongoose-base-repository');
const repository = new MongooseRepository('Log');
const jobName = 'Log job';
const logJob = (job, done) => {
repository.findOne({
text: 'test'
}).then((log) => {
console.log(`Log#: ${log.id}`);
done(); // Call this to inform once the job finished
}).catch((err) => {
console.log(`Error: ${err}`);
done(); // Call this to inform once the job finished
});
};
module.exports = (job) => {
// Job: Log something for test
job.define(jobName, logJob);
job.every('12 hours', jobName); // Schedule the job to run at every 12 hours
};
agenda module is being used here behind the scene which uses human interval for specifying the intervals. It supports the units: seconds
, minutes
, hours
, days
, weeks
, months
-- assumes 30 days, years
-- assumes 365 days.
As the schedule job uses mongodb to store the job configurations, application mongodb connection can be reused here with the below settings.
// Configuration
const settings = {
cronJobs: "./app/jobs/**/*.job.js",
mongoUrl: "mongodb://localhost/databaseName" // This can be overridden by "process.env.MONGO_URL"
};
Here, the same database is shared with the application. For better performance, different database or even different mongodb server instance can be used for the schedule jobs as below with the cronDbUrl
property.
// Configuration
const settings = {
cronJobs: "./app/jobs/**/*.job.js",
cronDbUrl: "mongodb://localhost/cron-db", // This can be overridden by "process.env.CRON_DB_URL"
mongoUrl: "mongodb://localhost/databaseName"
};
Default mongodb collection name is jobs
which can be changed by the cronDbCollection
property.
// Configuration
const settings = {
cronJobs: "./app/jobs/**/*.job.js",
cronDbUrl: "mongodb://localhost/cron-db", // This can be overridden by "process.env.CRON_DB_URL"
cronDbCollection: "cronjobs",
mongoUrl: "mongodb://localhost/databaseName"
};
More documentation to come through about advanced features. Please keep an eye on here.
Tests
npm test
License
The MIT License (MIT)
Copyright (c) 2017 iiWebi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.