Socket
Socket
Sign inDemoInstall

express-initialize

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-initialize

A simple Express app initializer


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

express-initialize

A simple Express app initializer.

Install it with NPM or Yarn:

$ npm install --save express-initialize
$ yarn add express-initialize

Example

Before

Everything is done in the app.js file:

// app/app.js

const express = require('express');
const handlebars = require('express-handlebars');
const orm = require('orm');

const app = express();

const hbs = handlebars.create({
    defaultLayout: 'main',
    helpers: {
        someFunction: () => { return 'Something'; }
    },
    // More configuration etc.
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

app.use(express.static('./static'))
app.use(compression());

app.get('/home', (request, response) => {
    response.render('home');
});

app.get('/other-page', (request, response) => {
    response.render('other-page');
});

orm.connect('mysql://root:password@host/database')
    .then(connection => {
        // etc.
    })
    .catch(console.error);

app.listen(80);
After

Everything is moved to separate files:

// app/app.js

const express = require('express');
const initialize = require('express-initialize');

const app = express();

initialize(app)
    .then(() => {
        app.listen(80);
    })
    .catch(console.error);

For example, a file for Handlebars.

// app/initializers/handlebars.js

module.exports = {
    configure: app => {
        const hbs = handlebars.create({
            defaultLayout: 'main',
            helpers: {
                someFunction: () => { return 'Something'; }
            },
            // More configuration etc.
        });
        app.engine('handlebars', hbs.engine);
        app.set('view engine', 'handlebars');
    }
};

You can also return a Promise.

// app/initializers/database.js

module.exports = {
    configure: app => {
        return new Promise((resolve, reject) => {
            orm.connect('mysql://root:password@host/database')
                .then(connection => {
                    // do something with connection
                    resolve();
                })
                .catch(reject);
        });
    }
}

Or specify what initializers have to run first.

// app/initializers/routes.js

module.exports = {
    after: 'middleware',
    configure: app => {
        app.get('/home', (request, response) => {
            response.render('home');
        });
    }
}

By default the name of an initializer is the file name (without .js). You can specify your own name by setting the name property.

FAQs

Package last updated on 05 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc