Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 618cd22

Browse files
Merge pull request #58 from Microsoft/makeImageboardLessBad
Improve imageboard sample
2 parents 397583b + 96224d8 commit 618cd22

File tree

11 files changed

+151
-2988
lines changed

11 files changed

+151
-2988
lines changed

imageboard/README.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,51 @@
44

55
This sample implements a complete Node.js application.
66
Notable features:
7-
- Typed usage of express for server side MVC
8-
- Typed usage of mongodb for server side database
9-
- Typed usage of Node.js
10-
- Use of TypeScript module syntax
11-
- Visual Studio project file for working with the project
7+
8+
* Typed usage of express for server side MVC
9+
* Typed usage of mongodb for server side database
10+
* Typed usage of Node.js
11+
* Use of external typings from DefinitelyTyped
12+
* Visual Studio project file for working with the project
1213

1314
## Running
1415

15-
Note: Perform steps 3 - 6 with your working directory set to the folder containing this README:
16+
Note: All commands entered need to be performed from within *this directory*.
1617

1718
1. Install MongoDB if necessary (see http://docs.mongodb.org/manual/installation/ )
1819

19-
2. Run the following command to launch the MongoDB process:
20-
`<mongoinstalldir>\bin\mongod`
21-
22-
3. Restore the sample app data to MongoDB in another command prompt with the following command:
23-
`<mongoinstalldir>\bin\mongorestore dump`
24-
25-
4. Install the app's node dependencies with the following command:
26-
`npm install`
27-
28-
5. Compile the app with the following command:
29-
`tsc --sourcemap --module commonjs app.ts`
30-
31-
6. Launch the Node process to serve the app using the following command:
32-
`node app.js`
33-
34-
7. Open your favorite browser and going to the following URL to access the app:
35-
`http://localhost:3000/`
20+
2. Ensure you have a clean directory to dedicate as to a database (e.g. `C:\imageboard` or `~/imageboard/`).
21+
22+
3. From *this repository's imageboard directory*, run the following command to launch the MongoDB process.
23+
```shell
24+
<MONGO_INSTALL_DIRECTORY>/bin/mongod --dbpath <PATH_TO_DB_DIRECTORY>
25+
```
26+
27+
4. From *this repository's imageboard directory*, restore the sample app data to MongoDB in another command prompt with the following command:
28+
```shell
29+
<MONGO_INSTALL_DIRECTORY>/bin/mongorestore dump
30+
```
31+
32+
5. From this imageboard directory, install the app's node dependencies, tsd, and typings with the following commands:
33+
```shell
34+
npm install
35+
npm install -g tsd
36+
tsd install
37+
```
38+
Some things to note:
39+
40+
* `npm install` will install this project's node dependencies from `package.json`.
41+
* `tsd install` will retrieve `.d.ts` files from [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped).
42+
43+
6. Compile the app with the following command:
44+
```shell
45+
tsc
46+
```
47+
The above command will use `tsconfig.json` to compile all necessary files.
48+
49+
7. Launch the Node process to serve the app using the following command:
50+
```shell
51+
node app.js
52+
```
53+
54+
7. Open your favorite browser and navigating to `http://localhost:3000/` to access the app.

imageboard/app.ts

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
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";
145
import errorHandler = require("errorhandler");
6+
import methodOverride = require("method-override");
7+
8+
import * as routes from "./routes/index";
9+
import * as db from "./db";
1510

1611
var app = express();
1712

@@ -27,9 +22,6 @@ app.use(express.static(__dirname + '/public'));
2722

2823
var env = process.env.NODE_ENV || 'development';
2924
if (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

3931
app.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 = /<img[^>]+src=[\"\']([^\'\"]+)[\"\']/g;
53-
var match, matches = [];
54-
while(match = re.exec(text)) {
43+
});
44+
45+
urlMessage.on('end', () => {
46+
console.log(text);
47+
const imageTagRegEx = /<img[^>]+src=[\"\']([^\'\"]+)[\"\']/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
});

imageboard/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"version": "0.0.1",
44
"private": true,
55
"dependencies": {
6-
"express": "4.11.1",
7-
"body-parser": "1.10.2",
8-
"errorhandler": "1.3.2",
9-
"method-override": "2.3.1",
6+
"express": "^4.13.3",
7+
"body-parser": "^1.14.1",
8+
"errorhandler": "^1.4.2",
9+
"method-override": "^2.3.5",
1010
"ejs": ">= 0.5.0",
11-
"jade": ">= 0.0.1",
11+
"jade": ">= 1.11.0",
1212
"mongodb": ">= 1.4.29"
1313
}
1414
}

imageboard/public/stylesheets/style.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ a
2121
color: #777777;
2222
}
2323

24-
.imglink
25-
{
26-
text: none;
27-
}
28-
2924
img.small
3025
{
3126
border:0;
@@ -70,6 +65,11 @@ img.large
7065
font-size: 80%;
7166
}
7267

68+
.title
69+
{
70+
font-size: 2.5rem;
71+
}
72+
7373
form
7474
{
7575
font-size: 18px;

imageboard/tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"sourceMap": true
5-
}
5+
},
6+
"files": [
7+
"./typings/tsd.d.ts",
8+
"./routes/index.ts",
9+
"./db.ts",
10+
"./app.ts"
11+
]
612
}

imageboard/tsd.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "v4",
3+
"repo": "borisyankov/DefinitelyTyped",
4+
"ref": "master",
5+
"path": "typings",
6+
"bundle": "typings/tsd.d.ts",
7+
"installed": {
8+
"mime/mime.d.ts": {
9+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
10+
},
11+
"serve-static/serve-static.d.ts": {
12+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
13+
},
14+
"express/express.d.ts": {
15+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
16+
},
17+
"mongodb/mongodb.d.ts": {
18+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
19+
},
20+
"node/node.d.ts": {
21+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
22+
},
23+
"method-override/method-override.d.ts": {
24+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
25+
},
26+
"errorhandler/errorhandler.d.ts": {
27+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
28+
},
29+
"body-parser/body-parser.d.ts": {
30+
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
31+
}
32+
}
33+
}

imageboard/typings/express/express-middleware.d.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)