easy-express

0.5.14 • Public • Published

What

Express with sockets (+sessions), session, file server (for yeoman fast debug), rpc: with just 1* line; Works out of the box

Install

npm install easy-express

Setup

  • MongoDB: Run "mongod" in order to use the mongodb database. Is required in order to work (for now).
  • Node

Use

Create a file named "index.js" with the following contents. Then run it like this "node index.js".

var defaultOptions = {

    /**
     * The type of environment. Based on this, if yeoman is enabled,
     * will add certain file paths to the server.
     * [development, production]
     *
     * You can also manually set certain file paths for each environment.
     */
    environment: 'development',

    /**
     * Sometimes you just want to start your node in a certain environment
     * just by appending on of the keywords ['production', 'development'].
     * This will override the environment passed in environment.
     *
     * @todo: should be better and should not only be in the index = 2.
     *
     * Set this value processArgv equal to the process.argv.
     *
     *     {
     *         extractEnvironmentFromCmdLineOptions : true,
     *         processArgv                          : process.argv
     *     }
     */
    extractEnvironmentFromCmdLineOptions: null,

    /**
     * Pass the process argv. Those are only used if
     * extractEnvironmentFromCmdLineOptions is true.
     */
    processArgv: null,


    /**
     * Host name
     */
    host: 'localhost',

    /**
     * Port of the host
     */
    port: 80,

    /**
     * Sockets
     */
    sockets: {
        enabled: true,

        /**
         * Url of the socket used in socket.io
         */
        url: '/socket',

        /**
         * Log level of the sockets.
         */
        logLevel: 1,
    },
    ssl: {
        enabled: false,
        key: null,
        cert: null,
        port: 443,
    },
    /**
     * Sessions
     */
    sessions: {
        enabled: true,

        /**
         * Type of session:
         *  - database: saves to a mongodb database by default
         *  - memory: saves in memory, bad practice and doesn't work with sockets
         */
        type: 'database',

        /**
         * The secret that encodes the sessions. (change this)
         */
        secret: 'random',

        /**
         * For how long lasts the sessions in milliseconds.
         */
        maxAge: 9000000,

        /**
         * Database of the sessions, when database is enabled.
         */
        database: {

            /**
             * Where the sessions are saved.
             */
            name: 'sessions',

            /**
             * Database host.
             */
            host: 'localhost',

            /**
             * Database port.
             */
            port: 27017,
        },
    },

    /**
     * Compress files for faster downloading.
     */
    enableGzip: true,

    /**
     * Used to get the post data in the requests.
     */
    enableBodyParser: true,

    /**
     * Serve files from path. Is recommend to be only used in a development
     * environment. In production use nginx, is faster and better.
     *
     * serveIndexByDefault:
     *     Whenever you want to serve the index.html from the static folder.
     *     Turn off when you want your express route to catch the "/" route
     *     and serve a specific html file or a dynamic one.
     *
     * Format:
     *     {
     *         path: 'my/path',
     *         serveIndexByDefault: false // Default true
     *     }
     */
    fileServer: {
        paths: []
    },

    /**
     * Enabling yeoman will add (not replace) your file server based on the environment.
     * If the environment is development will add 2 paths and in that order:
     *     /.tmp
     *     /app
     * If is production:
     *     /dist
     */
    yeoman: {
        enabled: true,

        /**
         * Root folder of the server, used to create a file server for
         * files in the .tmp, app and dist folder.
         */
        rootFolder: null,

        /**
         * Apply serveIndexByDefault to each yeoman folder.
         * Read fileServer for more info.
         */
        serveIndexByDefault: true,
    },

    /**
     * Using this will force the app to be in a certain way which is:
     *  - You can communicate both via sockets and post seamless;
     *  - The post path is always "/rpc"
     *  - The socket event name is always "rpc"
     *  - Services are called using the API on the client side and has the following
     *    format:
     *        {
     *            serviceName: String
     *            serviceArguments: Whatever you want
     *        }
     *  - The output of the service has the following format:
     *        {
     *            id: Number, only used in sockets to know which socket is for which request,
     *            data: data returned by the service,
     *            err: [errorMessage:String, errorNumber: Integer]
     *        }
     *  - In the case of sockets when they are connected will return the following data:
     *        {
     *            data: {connected: true},
     *            err: undefined
     *        }
     */
    rpc: {
        enabled: true,

        /**
         * This is the rpc app; The service name will always be a method on the rpcApp;
         */
         app: null,
    },
}


var innerDefaults = {
    fileServer: {
        path: {
            url                 : '',
            serveIndexByDefault : true,
        }
    }
}

Package Sidebar

Install

npm i easy-express

Weekly Downloads

2

Version

0.5.14

License

BSD-2-Clause

Last publish

Collaborators

  • totty90