UNPKG

7.13 kBMarkdownView Raw
1# WebAppEngine [![build status](https://travis-ci.org/cheton/webappengine.svg?branch=master)](https://travis-ci.org/cheton/webappengine) [![Coverage Status](https://coveralls.io/repos/cheton/webappengine/badge.svg?branch=master&service=github)](https://coveralls.io/github/cheton/webappengine?branch=master)
2
3[![NPM](https://nodei.co/npm/webappengine.png?downloads=true&stars=true)](https://nodei.co/npm/webappengine/)
4
5A web application platform that can host multiple web apps running with Node.js.
6
7![WebAppEngine](https://raw.githubusercontent.com/cheton/webappengine/master/media/screenshot.png)
8<i>Note. The administration UI is currently under construction.</i>
9
10## Installation
11For the API usage:
12```bash
13$ npm install --save webappengine
14```
15
16For the command line usage:
17```bash
18$ npm install -g webappengine
19```
20
21### Kickstart your project using [generator-webappengine](https://github.com/cheton/generator-webappengine)
22A Yeoman generator for developing WebAppEngine app is available at [https://github.com/cheton/generator-webappengine](https://github.com/cheton/generator-webappengine). You can use the generator to kickstart your project. It includes Gulp, Browserify, Babelify, Stylus, Handlebars, i18next, and React.
23
24Follow the steps to run the generator:
25```bash
26$ npm install -g yo
27$ npm install -g generator-webappengine
28$ yo webappengine
29```
30
31Once completed, you have to install NPM packages and Bower components, and run the `gulp` command to build your project.
32```bash
33$ npm install
34$ bower install
35$ gulp
36```
37
38Now you can run `node app/main.js` to launch your web app, or use webappengine to load [app.js](https://github.com/cheton/generator-webappengine/blob/master/generators/app/templates/app/app.js). For example:
39```js
40var path = require('path');
41var webappengine = require('webappengine');
42
43webappengine({
44 port: 80,
45 routes: [
46 {
47 type: 'server',
48 route: '/',
49 server: function(options) {
50 options = options || {};
51
52 var app = express();
53 var serveStatic = require('serve-static');
54 var assetPath = path.resolve(__dirname, 'web');
55
56 app.use(options.route, serveStatic(assetPath));
57
58 return app;
59 }
60 }
61 ]
62});
63```
64
65## Usage
66
67### API usage
68```js
69var path = require('path');
70var webappengine = require('webappengine');
71var options = {
72 port: 80, // [optional] The listen port (default: 8000)
73 //host: '', // [optional] The listen address or hostname (default: 0.0.0.0)
74 //backlog: 511, // [optional] The listen backlog (default: 511)
75 routes: [
76 {
77 type: 'static', // [static|server]
78 route: '/',
79 directory: path.resolve(__dirname, 'web') // for the static type
80 }
81 ]
82};
83
84webappengine(options)
85 .on('ready', function(server) {
86 var address = server.address();
87 console.log('Server is listening on %s:%d', address.address, address.port);
88
89 var io = require('socket.io')(server); // using socket.io
90 });
91```
92
93### Command line usage
94Run `webappengine` to start the app, and visit `http://yourhostname:8000/` to check if it works:
95
96```bash
97$ webappengine
98```
99
100To check what port the app is running on, find the message `Server is listening on 0.0.0.0:8000` from console output.
101
102By default the app listens on port 8000, you can run `webappengine` with `-p` (or `--port`) to determine which port your application should listen on. For example:
103```bash
104$ webappengine -p 80
105```
106
107Set the environment variable `NODE_ENV` to `production` if you are running in production mode:
108```bash
109$ NODE_ENV=production webappengine
110```
111
112Run `webappengine` with `-h` for detailed usage:
113```
114$ webappengine -h
115
116 Usage: webappengine [options]
117
118 Options:
119
120 -h, --help output usage information
121 -V, --version output the version number
122 -p, --port set listen port (default: 8000)
123 -H, --host <host> set listen address or hostname (default: 0.0.0.0)
124 -b, --backlog set listen backlog (default: 511)
125 -c, --config <filename> set multihost configuration file
126 -v, --verbose increase the verbosity level
127```
128
129## Getting Started
130
131### Working with static assets
132The following configuration will serve static assets from the directory:
133
134static-config.json:
135```json
136[
137 {
138 "type": "static",
139 "route": "/",
140 "directory": "/path/to/your/project/web/"
141 }
142]
143```
144
145Run `webappengine` with `--config` to set multihost configuration file:
146```bash
147$ webappengine --config "/path/to/your/project/static-config.json"
148```
149
150or use the API:
151```js
152var webappengine = require('webappengine');
153var routes = require('./static-config.json');
154
155webappengine({
156 routes: routes
157});
158```
159
160Visits `http://yourhostname:8000/` will serve `index.html` file as below:
161```
162<h1>WebAppEngine Test Page</h1>
163```
164(See also: [examples/static/index.html](examples/static/index.html))
165
166### Configure multihost settings to run multiple web apps
167First, checkout [examples/simple/app.js](examples/simple/app.js) and [examples/multihost.json](examples/multihost.json), and copy [examples](examples) to your project folder to kickstart a web application.
168
169simple/app.js:
170```js
171var path = require('path'),
172 express = require('express');
173
174module.exports = function(options) {
175 options = options || {};
176
177 var app = express();
178 var serveStatic = require('serve-static');
179 var assetPath = path.resolve(__dirname, 'web');
180
181 // Enable case sensitivity routing: "/Foo" is not equal to "/foo"
182 app.enable('case sensitive routing');
183 // Disable strict routing: "/foo" and "/foo/" are treated the same
184 app.disable('strict routing');
185
186 app.use(options.route, serveStatic(assetPath));
187
188 return app;
189};
190```
191
192server-config.json:
193```json
194[
195 {
196 "type": "server",
197 "route": "/simple",
198 "server": "/path/to/your/project/simple/app"
199 }
200]
201```
202
203Run `webappengine` with `--config` to set multihost configuration file:
204```bash
205$ webappengine --config "/path/to/your/project/server-config.json"
206```
207
208or use the API:
209```js
210var webappengine = require('webappengine');
211var routes = require('./server-config.json');
212
213webappengine({
214 routes: routes
215});
216```
217
218Visits `http://yourhostname:8000/simple` will serve `index.html` file as below:
219```
220<h1>WebAppEngine Test Page</h1>
221```
222(See also: [examples/simple/web/index.html](examples/simple/web/index.html))
223
224## Administration UI
225<i>The administration UI is currently under construction.</i>
226### Dashboard
227TBD
228### Change the display language
229You can change the display language from the <b>Settings</b> menu, it will set the `lang` query string parameter: `?lang={locale}`
230
231Here is a list of currently supported locales:
232
233Locale | Language
234------ | --------
235de | Deutsch
236en | English (US)
237es | Español
238fr | Français
239it | Italiano
240ja | 日本語
241zh-cn | 中文 (简体)
242zh-tw | 中文 (繁體)
243
244## License
245
246Copyright (c) 2015-2016 Cheton Wu
247
248Licensed under the [MIT License](LICENSE).