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" ;
145import errorHandler = require( "errorhandler" ) ;
6+ import methodOverride = require( "method-override" ) ;
7+
8+ import * as routes from "./routes/index" ;
9+ import * as db from "./db" ;
1510
1611var app = express ( ) ;
1712
@@ -27,9 +22,6 @@ app.use(express.static(__dirname + '/public'));
2722
2823var env = process . env . NODE_ENV || 'development' ;
2924if ( env === 'development' ) {
30- app . use ( errorHandler ( { dumpExceptions : true , showStack : true } ) ) ;
31- }
32- else if ( env === 'production' ) {
3325 app . use ( errorHandler ( ) ) ;
3426}
3527
@@ -38,91 +30,98 @@ else if (env === 'production') {
3830
3931app . get ( '/' , routes . index ) ;
4032
41- app . get ( '/findImages' , function ( req , res ) {
33+ app . get ( '/findImages' , ( req , res ) => {
4234 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 ) => {
4842 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 ) ) {
5552 matches . push ( match [ 1 ] ) ;
5653 }
54+
5755 res . write ( JSON . stringify ( matches ) ) ;
5856 res . end ( ) ;
5957 } ) ;
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+ } ) ;
6362} ) ;
6463
65- app . get ( '/user/:userid' , function ( req , res ) {
64+ app . get ( '/user/:userid' , ( req , res ) => {
6665 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' , {
6968 title : user . _id ,
70- username : user . _id ,
71- boards : user . boards
69+ username : user . _id ,
70+ boards : user . boards
7271 } ) ;
7372 } ) ;
7473} ) ;
7574
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' , {
7877 username : req . params . userid
79- } ) ;
78+ } ) ;
8079} ) ;
8180
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 ) ;
8584 } ) ;
8685} ) ;
8786
88- app . get ( '/user/:userid/:boardid' , function ( req , res ) {
87+ app . get ( '/user/:userid/:boardid' , ( req , res ) => {
8988 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' , {
9795 title : user . _id ,
98- username : user . _id ,
96+ username : user . _id ,
9997 board : board ,
10098 images : images
10199 } ) ;
102100 } ) ;
103- } else {
101+ }
102+ else {
104103 res . send ( 404 , 'not found' ) ;
105104 }
106105 } ) ;
107106} ) ;
108107
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' , {
111110 username : req . params . userid ,
112111 boardid : req . params . boardid
113- } ) ;
112+ } ) ;
114113} ) ;
115114
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 => {
118117 res . redirect ( '/user/' + req . params . userid + "/" + req . params . boardid )
119118 } ) ;
120119} ) ;
121120
122- app . get ( '/image/:imageid' , function ( req , res ) {
121+ app . get ( '/image/:imageid' , ( req , res ) => {
123122 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' , {
126125 title : "image" ,
127126 image : image
128127 } ) ;
0 commit comments