1 | #Express Server
|
2 |
|
3 | Express Server is an easy to use node.js webserver based on express.js
|
4 |
|
5 |
|
6 |
|
7 | ##Integration
|
8 | Create folowing folder structure in your project root:
|
9 | All these files are optional.
|
10 |
|
11 | ```
|
12 | projectRoot
|
13 | --------------------------
|
14 | +-- server
|
15 | +-- env
|
16 | +-- development.js //Dev config
|
17 | +-- production.js //Production config
|
18 | +-- database.js //Database connections
|
19 | +-- express.js //Express config and adons
|
20 | +-- init.js //To be called when server was started
|
21 | +-- routes
|
22 | +-- myRoutes.js //Your routes
|
23 | +-- moreRoutes.js
|
24 | ```
|
25 |
|
26 | ##Start an express server
|
27 |
|
28 | ```
|
29 | var server = new ExpressServer({
|
30 | name: 'My Express Server',
|
31 | port: 3000
|
32 | });
|
33 | ```
|
34 |
|
35 | ##Setting up env configurations
|
36 |
|
37 | ```
|
38 | module.exports = function(app, done) {
|
39 | 'use strict';
|
40 |
|
41 | app.use(function(req, res, next){
|
42 | //Do something
|
43 | next();
|
44 | });
|
45 |
|
46 | done();
|
47 | };
|
48 | ```
|
49 |
|
50 | Don't forget to call the done function
|
51 |
|
52 |
|
53 | ##Connecting to a database
|
54 |
|
55 | ```
|
56 | module.exports = function(app, done) {
|
57 | 'use strict';
|
58 |
|
59 | //Make db connection
|
60 |
|
61 | done();
|
62 | };
|
63 | ```
|
64 |
|
65 | ##Using express middlewares
|
66 |
|
67 | ```
|
68 | module.exports = function(app, done) {
|
69 | 'use strict';
|
70 |
|
71 | app.use(anyMiddleware);
|
72 |
|
73 | done();
|
74 | };
|
75 | ```
|
76 |
|
77 | ##Init script
|
78 |
|
79 | ```
|
80 | module.exports = function(app, done) {
|
81 | 'use strict';
|
82 |
|
83 | //Doing something during server start progress
|
84 |
|
85 | done();
|
86 | };
|
87 | ```
|
88 |
|
89 | ##Adding routes
|
90 |
|
91 | Place routes under yourproject/routes/
|
92 | All files in this folder will be called during the start progress
|
93 |
|
94 |
|
95 | ```
|
96 | module.exports = function(app, done) {
|
97 | 'use strict';
|
98 |
|
99 | app.get('/', function(req, res, next) {
|
100 | res.send('Hello World!');
|
101 | next();
|
102 | });
|
103 |
|
104 | done();
|
105 | };
|
106 | ``` |
\ | No newline at end of file |