-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (53 loc) · 2.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const express = require( "express" );
const fs = require( 'fs' );
const nPath = require( 'path' );
const bParser = require( 'body-parser' );
const app = express();
const port = 8090;
const MongoDbConnection = require( './api/MongoDbConnection' );
function importJsFiles ( path, succReg ) {
try {
let isDir = fs.statSync( path ).isDirectory();
if ( isDir ) {
fs.readdirSync( path ).forEach( childName => {
importJsFiles( nPath.join( path, childName ), succReg );
} );
} else {
if ( path.toLowerCase().endsWith( 'api.js' ) ) {
require( path )( app, express.Router() );
succReg.push( path.replace( __dirname, '.' ) );
}
// else {
// console.log( `[registerApi] register files named as 'XXXXXXApi.js' under directory. Invalid file = [${path.replace( __dirname, '.' )}] <<< SKIPPING >>>` );
// }
}
} catch ( ex ) {
console.error( ex );
}
}
module.exports = () => {
return {
instance: app,
registerApi: ( ...filePaths ) => {
const succReg = [];
filePaths.forEach( p => {
let dJoin = nPath.join( __dirname, p );
try {
importJsFiles( dJoin, succReg );
} catch ( err ) {
console.log( `[registerApi] accepts only directory path/exact file path. Invalid path = [${p}] <<< SKIPPING >>>` );
}
console.log( `Register successfully = [${succReg.join(', ')}]` );
} );
},
start: function () {
app.listen( port, () => console.log( `listening on port ${port}` ) );
MongoDbConnection.startup();
app.use( bParser.json() );
app.use( bParser( {} ) );
app.get( '/form', ( rq, rs ) => {
rs.sendfile( './z.html' );
} )
}
}
};