1
- /// <reference path='typings/node/node.d.ts' />
2
- /// <reference path='typings/mongodb/mongodb.d.ts' />
3
- /// <reference path='typings/express/express.d.ts' />
4
- /// <reference path='typings/express/express-middleware.d.ts' />
5
-
6
-
7
- import http = require( "http" )
8
- import url = require( "url" )
9
- import routes = require( "./routes/index" )
10
- import db = require( "./db" )
11
- import express = require( "express" )
12
- import bodyParser = require( "body-parser" ) ;
13
- import methodOverride = require( "method-override" ) ;
1
+ import * as http from "http" ;
2
+ import * as url from "url" ;
3
+ import * as express from "express" ;
4
+ import * as bodyParser from "body-parser" ;
14
5
import errorHandler = require( "errorhandler" ) ;
6
+ import methodOverride = require( "method-override" ) ;
7
+
8
+ import * as routes from "./routes/index" ;
9
+ import * as db from "./db" ;
15
10
16
11
var app = express ( ) ;
17
12
@@ -27,9 +22,6 @@ app.use(express.static(__dirname + '/public'));
27
22
28
23
var env = process . env . NODE_ENV || 'development' ;
29
24
if ( env === 'development' ) {
30
- app . use ( errorHandler ( { dumpExceptions : true , showStack : true } ) ) ;
31
- }
32
- else if ( env === 'production' ) {
33
25
app . use ( errorHandler ( ) ) ;
34
26
}
35
27
@@ -38,91 +30,98 @@ else if (env === 'production') {
38
30
39
31
app . get ( '/' , routes . index ) ;
40
32
41
- app . get ( '/findImages' , function ( req , res ) {
33
+ app . get ( '/findImages' , ( req , res ) => {
42
34
console . log ( 'getting images from' + req . query [ 'url' ] ) ;
43
-
44
- var req2 = http . get ( url . parse ( req . query [ 'url' ] ) , function ( urlres ) {
45
- console . log ( "Got response: " + urlres . statusCode ) ;
46
- var text = "" ;
47
- urlres . on ( 'data' , function ( chunk : string ) {
35
+
36
+ let req2 = http . get ( url . parse ( req . query [ 'url' ] ) , urlMessage => {
37
+ console . log ( "Got response: " + urlMessage . statusCode ) ;
38
+
39
+ let text = "" ;
40
+
41
+ urlMessage . on ( 'data' , ( chunk : string ) => {
48
42
text += chunk ;
49
- } ) ;
50
- urlres . on ( 'end' , function ( ) {
51
- console . log ( text ) ;
52
- var re = / < i m g [ ^ > ] + s r c = [ \" \' ] ( [ ^ \' \" ] + ) [ \" \' ] / g;
53
- var match , matches = [ ] ;
54
- while ( match = re . exec ( text ) ) {
43
+ } ) ;
44
+
45
+ urlMessage . on ( 'end' , ( ) => {
46
+ console . log ( text ) ;
47
+ const imageTagRegEx = / < i m g [ ^ > ] + s r c = [ \" \' ] ( [ ^ \' \" ] + ) [ \" \' ] / g;
48
+
49
+ let match : RegExpMatchArray ;
50
+ let matches : string [ ] = [ ] ;
51
+ while ( match = imageTagRegEx . exec ( text ) ) {
55
52
matches . push ( match [ 1 ] ) ;
56
53
}
54
+
57
55
res . write ( JSON . stringify ( matches ) ) ;
58
56
res . end ( ) ;
59
57
} ) ;
60
- } ) . on ( 'error' , function ( a , e ) {
61
- console . log ( "Got error: " + e . message ) ;
62
- } ) ;
58
+
59
+ } ) . on ( 'error' , function ( a , e ) {
60
+ console . log ( "Got error: " + e . message ) ;
61
+ } ) ;
63
62
} ) ;
64
63
65
- app . get ( '/user/:userid' , function ( req , res ) {
64
+ app . get ( '/user/:userid' , ( req , res ) => {
66
65
console . log ( 'getting user ' + req . params . userid ) ;
67
- db . getUser ( req . params . userid , function ( user ) {
68
- res . render ( 'user' , {
66
+ db . getUser ( req . params . userid , user => {
67
+ res . render ( 'user' , {
69
68
title : user . _id ,
70
- username : user . _id ,
71
- boards : user . boards
69
+ username : user . _id ,
70
+ boards : user . boards
72
71
} ) ;
73
72
} ) ;
74
73
} ) ;
75
74
76
- app . get ( '/user/:userid/newboard' , function ( req , res ) {
77
- res . render ( 'newboard' , {
75
+ app . get ( '/user/:userid/newboard' , ( req , res ) => {
76
+ res . render ( 'newboard' , {
78
77
username : req . params . userid
79
- } ) ;
78
+ } ) ;
80
79
} ) ;
81
80
82
- app . post ( '/user/:userid/newboard' , function ( req , res ) {
83
- db . addBoard ( req . params . userid , req . param ( 'title' ) , req . param ( 'description' ) , function ( user ) {
84
- res . redirect ( '/user/' + req . params . userid )
81
+ app . post ( '/user/:userid/newboard' , ( req , res ) => {
82
+ db . addBoard ( req . params . userid , req . param ( 'title' ) , req . param ( 'description' ) , user => {
83
+ res . redirect ( '/user/' + req . params . userid ) ;
85
84
} ) ;
86
85
} ) ;
87
86
88
- app . get ( '/user/:userid/:boardid' , function ( req , res ) {
87
+ app . get ( '/user/:userid/:boardid' , ( req , res ) => {
89
88
console . log ( 'getting ' + req . params . userid + ", " + req . params . boardid ) ;
90
- db . getUser ( req . params . userid , function ( user ) {
91
- var board = user . boards . filter ( function ( board ) {
92
- return decodeURIComponent ( req . params . boardid ) === board . title ;
93
- } ) [ 0 ] ;
94
- if ( board ) {
95
- db . getImages ( board . images , function ( images ) {
96
- res . render ( 'board' , {
89
+ db . getUser ( req . params . userid , user => {
90
+ let board = user . boards . filter ( board => decodeURIComponent ( req . params . boardid ) === board . title ) [ 0 ] ;
91
+
92
+ if ( board ) {
93
+ db . getImages ( board . images , images => {
94
+ res . render ( 'board' , {
97
95
title : user . _id ,
98
- username : user . _id ,
96
+ username : user . _id ,
99
97
board : board ,
100
98
images : images
101
99
} ) ;
102
100
} ) ;
103
- } else {
101
+ }
102
+ else {
104
103
res . send ( 404 , 'not found' ) ;
105
104
}
106
105
} ) ;
107
106
} ) ;
108
107
109
- app . get ( '/user/:userid/:boardid/newpin' , function ( req , res ) {
110
- res . render ( 'newpin' , {
108
+ app . get ( '/user/:userid/:boardid/newpin' , ( req , res ) => {
109
+ res . render ( 'newpin' , {
111
110
username : req . params . userid ,
112
111
boardid : req . params . boardid
113
- } ) ;
112
+ } ) ;
114
113
} ) ;
115
114
116
- app . post ( '/user/:userid/:boardid/newpin' , function ( req , res ) {
117
- db . addPin ( req . params . userid , req . params . boardid , req . param ( 'imageUri' ) , req . param ( 'link' ) , req . param ( 'caption' ) , function ( user ) {
115
+ app . post ( '/user/:userid/:boardid/newpin' , ( req , res ) => {
116
+ db . addPin ( req . params . userid , req . params . boardid , req . param ( 'imageUri' ) , req . param ( 'link' ) , req . param ( 'caption' ) , user => {
118
117
res . redirect ( '/user/' + req . params . userid + "/" + req . params . boardid )
119
118
} ) ;
120
119
} ) ;
121
120
122
- app . get ( '/image/:imageid' , function ( req , res ) {
121
+ app . get ( '/image/:imageid' , ( req , res ) => {
123
122
console . log ( 'getting image ' + req . params . imageid ) ;
124
- db . getImage ( req . params . imageid , function ( image ) {
125
- res . render ( 'image' , {
123
+ db . getImage ( req . params . imageid , image => {
124
+ res . render ( 'image' , {
126
125
title : "image" ,
127
126
image : image
128
127
} ) ;
0 commit comments