UNPKG

2.18 kBMarkdownView Raw
1#Express Server
2
3Express Server is an easy to use node.js webserver based on express.js
4
5
6
7##Integration
8Create folowing folder structure in your project root:
9All these files are optional.
10
11```
12projectRoot
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```
29var server = new ExpressServer({
30 name: 'My Express Server',
31 port: 3000
32});
33
34```
35
36###Options:
37 name Set server name
38 baseDir Set base dir. Default is process.cwd()
39 confDir Path to config files from dir (Load confiles from $baseDir/conf, $baseDir/../conf/ per default)
40 port Set default port (Default: 3000)
41 requestLog Enable request logging. This value can be a boolean or a string.
42 A String is interpreted as the log file path.
43 (Default: <baseDir>/log/request.log). Request logging is disabled
44 by default
45 apiInfo Enables api info. Set a path where the infos should be shown
46
47##Setting up env configurations
48
49```
50module.exports = function(app) {
51 'use strict';
52
53 app.use(function(req, res, next){
54 //Do something
55 next();
56 });
57};
58```
59
60##Connecting to a database
61
62```
63module.exports = function(app) {
64 'use strict';
65
66 //Make db connection, return a promise to make an async call
67
68 return new Promise((resolve, reject) => {
69 db.connect((err) => {
70 if (err) {
71 reject(err);
72 return;
73 }
74
75 resolve();
76 });
77 });
78};
79```
80
81##Using express middlewares
82
83```
84module.exports = function(app) {
85 'use strict';
86
87 app.use(anyMiddleware);
88};
89```
90
91##Init script
92
93```
94module.exports = function(app) {
95 'use strict';
96
97 //Doing something during server start progress
98};
99```
100
101##Adding routes
102
103Place routes under yourproject/routes/
104All files in this folder will be called during the start progress
105
106
107```
108module.exports = function(app) {
109 'use strict';
110
111 app.get('/', function(req, res, next) {
112 res.send('Hello World!');
113 next();
114 });
115};
116```