1
1
const fs = require ( "fs" ) ;
2
2
const path = require ( "path" ) ;
3
- const { ObjectId } = require ( "mongodb" ) ;
4
3
5
- // TODO: fix to npm dependency
4
+ const { server , initConfLog , codes } = require ( "server-framework" ) ;
6
5
7
- const {
8
- server,
9
- initConfLog,
10
- initMongoDb,
11
- codes
12
- } = require ( "server-framework" ) ;
6
+ const protectedMiddleware = require ( "./middlewares/protectedMiddleware" ) ;
7
+ const productRoutes = require ( "./routes/products" ) ;
8
+ const { defaultJsonParser } = require ( "./utils" ) ;
13
9
14
-
15
- const configPath = path . join ( __dirname , "config.json" ) ;
16
-
17
- // path.join(__dirname, "logs", "error.log");
18
-
19
- // TODO: https://blog.logrocket.com/organizing-express-js-project-structure-better-productivity/
20
- // https://github.com/geshan/expressjs-structure
21
-
22
- const defaultJsonParser = ( body ) => {
23
- // FST_ERR_CTP_EMPTY_JSON_BODY
24
-
25
- body = body ? JSON . parse ( body ) : { } ;
26
- return body ;
27
- } ;
10
+ const configPath = path . join ( __dirname , "configs" , "config.json" ) ;
11
+ const logsFolderPath = path . join ( __dirname , "logs" ) ;
28
12
29
13
const start = async ( ) => {
30
-
31
- const { logger, config } = initConfLog ( configPath ) ;
32
- const dbConfig = config . get ( "dbConfig" ) ;
14
+
15
+ const { logger, config } = initConfLog ( configPath , logsFolderPath ) ;
33
16
const appPort = config . get ( "appPort" ) ;
34
- const collName = dbConfig . collName ;
35
- const db = await initMongoDb ( ) ;
36
- const app = server ( ) ;
37
17
18
+ const app = server ( ) ;
38
19
app . addContentTypeParser ( "application/json" , defaultJsonParser ) ;
39
20
40
- app . get ( "/products/:id" , async ( req , res ) => {
41
- // /products/1, req.params has value { id: '1' }
42
- try {
43
- const dbResponse = await db
44
- . collection ( collName )
45
- . findOne ( { _id : new ObjectId ( req . params . id ) } ) ;
46
- logger . info ( "res" , dbResponse ) ;
21
+ const router = app . Router ;
47
22
48
- res . json ( dbResponse ) ;
49
- } catch ( e ) {
50
- console . log ( e ) ;
23
+ productRoutes ( router ) ;
51
24
52
- throw codes . INTERNAL_SERVER_ERROR ;
53
- }
54
- } ) ;
55
-
56
- app . get ( "/products/" , ( req , res ) => {
57
- // /products?page=1&pageSize=10 => { page: '1', pageSize: '10' }
58
- logger . info ( req . query ) ;
59
- res . send ( JSON . stringify ( req . query ) ) ;
60
- } ) ;
61
-
62
- app . post ( "/products/" , async ( req , res ) => {
63
- const data = req . body ;
64
- logger . info ( "data" , data ) ;
65
- try {
66
- const dbResponse = await db . collection ( collName ) . insertOne ( data ) ;
67
-
68
- logger . info ( `dbResponse insertOne: ${ dbResponse . insertedId . toString ( ) } ` ) ;
69
- res . send ( dbResponse . insertedId . toString ( ) ) ;
70
- } catch ( e ) {
71
- console . log ( e ) ;
72
- throw codes . INTERNAL_SERVER_ERROR ;
73
- }
74
- } ) ;
75
-
76
- app . get ( "/photos/:file" , function ( req , res ) {
77
- // static
25
+ // static
26
+ router . get ( "/photos/:file" , function ( req , res ) {
78
27
const file = req . params . file ;
79
28
const pth = path . join ( __dirname , "uploads" , file + ".jpeg" ) ;
80
29
@@ -86,15 +35,8 @@ const start = async () => {
86
35
}
87
36
} ) ;
88
37
89
- const protectedMiddleware = ( req , res , next ) => {
90
- if ( req . headers [ "authorization" ] === "a123" ) {
91
- next ( ) ;
92
- } else {
93
- throw codes . UNAUTHORIZED ;
94
- }
95
- } ;
96
-
97
- app . get ( "/protected" , protectedMiddleware , ( req , res ) => {
38
+ // middleware example
39
+ router . get ( "/protected" , protectedMiddleware , ( req , res ) => {
98
40
res . end ( "protected route" ) ;
99
41
} ) ;
100
42
0 commit comments