├── .gitignore
├── lesson-09
├── test
│ ├── user1.json
│ ├── user2.json
│ └── adduser
├── README.md
├── package.json
└── app.js
├── lesson-10
├── test
│ ├── user1.json
│ ├── user2.json
│ └── adduser
├── README.md
├── package.json
└── app.js
├── lesson-01
├── README.md
└── app.js
├── lesson-02
├── index.html
├── README.md
└── app.js
├── lesson-03-B
├── README.md
├── package.json
└── app.js
├── lesson-03-C
├── public
│ └── index.html
├── README.md
├── package.json
└── app.js
├── lesson-05
├── README.md
├── package.json
└── app.js
├── lesson-04
├── README.md
├── app.js
└── package.json
├── lesson-06
├── README.md
├── package.json
└── app.js
├── lesson-03-A
├── README.md
├── package.json
└── app.js
├── lesson-07
├── README.md
├── package.json
└── app.js
├── lesson-08
├── README.md
├── package.json
└── app.js
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
3 |
--------------------------------------------------------------------------------
/lesson-09/test/user1.json:
--------------------------------------------------------------------------------
1 | {
2 | "name" : "tony"
3 | }
--------------------------------------------------------------------------------
/lesson-09/test/user2.json:
--------------------------------------------------------------------------------
1 | {
2 | "name" : "tenny"
3 | }
--------------------------------------------------------------------------------
/lesson-10/test/user1.json:
--------------------------------------------------------------------------------
1 | {
2 | "name" : "tony"
3 | }
--------------------------------------------------------------------------------
/lesson-10/test/user2.json:
--------------------------------------------------------------------------------
1 | {
2 | "name" : "tenny"
3 | }
--------------------------------------------------------------------------------
/lesson-01/README.md:
--------------------------------------------------------------------------------
1 | Simple Node.js server that responds with 'hello world'
--------------------------------------------------------------------------------
/lesson-02/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello world!
4 |
5 |
--------------------------------------------------------------------------------
/lesson-03-B/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app with multiple middleware functions.
--------------------------------------------------------------------------------
/lesson-09/README.md:
--------------------------------------------------------------------------------
1 | Simple Express example for serving an API.
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/lesson-10/README.md:
--------------------------------------------------------------------------------
1 | Simple Express example for serving an API.
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/lesson-03-C/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello world!
4 |
5 |
--------------------------------------------------------------------------------
/lesson-02/README.md:
--------------------------------------------------------------------------------
1 | Simple Node.js http server that reads index.html from the file system.
--------------------------------------------------------------------------------
/lesson-03-C/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app with a popular middleware for serving static files.
--------------------------------------------------------------------------------
/lesson-05/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app that demonstrates waiting for server 'listen' event.
2 |
--------------------------------------------------------------------------------
/lesson-04/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app that responds with 'hello world' but only handles one route (/).
--------------------------------------------------------------------------------
/lesson-06/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app that demonstrates specifying address as well as the port.
2 |
--------------------------------------------------------------------------------
/lesson-09/test/adduser:
--------------------------------------------------------------------------------
1 | curl -X POST -H "Content-type: application/json" -d @$1 http://localhost:3000/users
--------------------------------------------------------------------------------
/lesson-10/test/adduser:
--------------------------------------------------------------------------------
1 | curl -X POST -H "Content-type: application/json" -d @$1 http://localhost:3000/users
--------------------------------------------------------------------------------
/lesson-03-A/README.md:
--------------------------------------------------------------------------------
1 | Simple Express app with a middleware function to respond with 'hello world' for any request.
--------------------------------------------------------------------------------
/lesson-07/README.md:
--------------------------------------------------------------------------------
1 | Simple Express routing example.
2 |
3 | Can only access the following routes:
4 | * /
5 | * /about
6 | * /hello
7 |
8 |
--------------------------------------------------------------------------------
/lesson-08/README.md:
--------------------------------------------------------------------------------
1 | Simple Express routing example that returns different content type responses.
2 |
3 | Can only access the following routes:
4 | * /hello1
5 | * /hello2
6 | * /hello3
7 | * /hello4
8 | * /hello5
9 | * /hello6
10 |
11 |
12 | Ref:
13 |
14 | * http://expressjs.com/4x/api.html#res.send
15 |
16 |
--------------------------------------------------------------------------------
/lesson-01/app.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 |
3 | function onListenEvent(req, res) {
4 | res.writeHead(200, { 'Content-Type': 'text/html' });
5 | res.end('hello world
');
6 | }
7 |
8 | var app = http.createServer(onListenEvent);
9 |
10 | app.listen(3000, 'localhost');
11 |
12 | console.log('server app running at localhost:3000');
13 |
14 |
--------------------------------------------------------------------------------
/lesson-07/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-07",
3 | "version": "1.0.0",
4 | "description": "Simple Express routing example",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-04/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var express = require('express'),
3 | app = express(),
4 | port = process.env.PORT || 3000;
5 |
6 | // route handler for GET /
7 | app.get('/', function(req, res) {
8 | var data = 'hello world
';
9 | res.send(data);
10 | });
11 |
12 | app.listen(port);
13 |
14 | console.log('server started on port %s', port);
15 |
16 |
--------------------------------------------------------------------------------
/lesson-05/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-05",
3 | "version": "1.0.0",
4 | "description": "Simple Express app that demonstrates waiting for server 'listen' event",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-06/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-05",
3 | "version": "1.0.0",
4 | "description": "Simple Express app that demonstrates specifying address as well as the port.",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-08/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-08",
3 | "version": "1.0.0",
4 | "description": "Simple Express example that serves responses with different content type",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-03-B/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-03-B",
3 | "version": "1.0.0",
4 | "description": "Simple Express app with multiple middleware functions",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7",
12 | "morgan": "^1.3.2"
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/lesson-04/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-04",
3 | "version": "1.0.0",
4 | "description": "Simple Express app that responds with 'hello world' but only handles one route (/)",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-03-A/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-03-A",
3 | "version": "1.0.0",
4 | "description": "Simple Express app with a middleware function to respond with 'hello world' for any request",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/lesson-07/app.js:
--------------------------------------------------------------------------------
1 | var app = require('express')();
2 | port = process.env.PORT || 3000;
3 |
4 | app.get('/', function(req, res) {
5 | res.send('Welcome to Express');
6 | });
7 |
8 | app.get('/about', function(req, res) {
9 | res.send('This is just a simple Express routing demo');
10 | });
11 |
12 | app.get('/hello', function(req, res) {
13 | res.send('Well, hello there!');
14 | });
15 |
16 | app.listen(port);
17 |
18 |
--------------------------------------------------------------------------------
/lesson-03-C/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-03-C",
3 | "version": "1.0.0",
4 | "description": "Simple Express app with a popular middleware for serving static files",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "express": "^4.9.7",
12 | "morgan": "^1.3.2"
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/lesson-03-A/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var express = require('express'),
3 | app = express(),
4 | port = process.env.PORT || 3000;
5 |
6 | // create express middleware
7 | app.use(function(req, res) {
8 | var data = 'hello world
';
9 |
10 | res.writeHead(200, { 'Content-Type': 'text/html' });
11 | res.end(data);
12 | });
13 |
14 | app.listen(port);
15 |
16 | console.log('server started on port %s', port);
17 |
18 |
--------------------------------------------------------------------------------
/lesson-09/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-09",
3 | "version": "1.0.0",
4 | "description": "Simple Express example with an API",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "body-parser": "^1.9.0",
12 | "express": "^4.9.7",
13 | "morgan": "^1.3.2",
14 | "underscore": "^1.7.0"
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/lesson-10/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lesson-10",
3 | "version": "1.0.0",
4 | "description": "Simple Express Hello World App",
5 | "author": "Tony Pujals (http://twitter.com/subfuzion)",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node app.js"
9 | },
10 | "dependencies": {
11 | "body-parser": "^1.9.0",
12 | "express": "^4.9.7",
13 | "morgan": "^1.3.2",
14 | "underscore": "^1.7.0",
15 | "mongodb": "^1.4.19"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/lesson-06/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var app = require('express')(),
3 | port = process.env.PORT || 3000;
4 |
5 | // route handler for GET /
6 | app.get('/', function(req, res) {
7 | var data = 'hello world
';
8 | res.send(data);
9 | });
10 |
11 | // can specify port and address/hostname
12 | // see: http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
13 | var server = app.listen(port, '127.0.0.1', function() {
14 | var host = server.address();
15 | console.log('server started on %s:%s', host.address, host.port);
16 | });
17 |
18 |
19 |
--------------------------------------------------------------------------------
/lesson-05/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var app = require('express')(),
3 | port = process.env.PORT || 3000;
4 |
5 | // route handler for GET /
6 | app.get('/', function(req, res) {
7 | var data = 'hello world
';
8 | res.send(data);
9 | });
10 |
11 | var server = app.listen(port, function() {
12 | // the callback is added as a listener for the server's 'listen' event
13 | // see: http://nodejs.org/api/net.html#net_server_listen_path_callback
14 |
15 | // server is an http.Server, which is a net.Server
16 | // http://nodejs.org/api/http.html#http_class_http_server
17 | // http://nodejs.org/api/net.html#net_class_net_server
18 | // http://nodejs.org/api/net.html#net_server_address
19 | console.log('server started on port %s', server.address().port);
20 | });
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/lesson-03-C/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var express = require('express'),
3 | app = express(),
4 | morgan = require('morgan'),
5 | port = process.env.PORT || 3000,
6 | publicDir = require('path').join(__dirname, '/public');
7 |
8 |
9 | // add logging middleware
10 | app.use(morgan('dev'));
11 |
12 | // add the express.static middleware to the app to
13 | // serve files in the specified path
14 | // This middleware will only call the next middleware if
15 | // path doesn't match the static directory
16 | app.use(express.static(publicDir));
17 |
18 |
19 | // express middleware that handles remaining requests for non-static files
20 | app.use(function(req, res) {
21 | var data = 'Nothing here for you...
';
22 |
23 | res.writeHead(200, { 'Content-Type': 'text/html' });
24 | res.end(data);
25 | });
26 |
27 | app.listen(port);
28 |
29 | console.log('server started on port %s', port);
30 |
31 |
--------------------------------------------------------------------------------
/lesson-02/app.js:
--------------------------------------------------------------------------------
1 | var http = require('http'),
2 | fs = require('fs'),
3 | path = require('path');
4 |
5 | var app = http.createServer(function(req, res) {
6 | var index = path.join(__dirname, 'index.html');
7 |
8 | if (req.url == '/' || req.url == '/index.html') {
9 |
10 | fs.readFile(index, function(err, data) {
11 | if (err) {
12 | console.error(err);
13 | res.writeHead(500, {'Content-Type': 'text/html'});
14 | res.end('500 server error');
15 | } else {
16 | res.writeHead(200, {'Content-Type': 'text/html'});
17 | res.end(data);
18 | }
19 | })
20 |
21 | } else {
22 | // resource not found
23 | console.log('resource not found: ' + req.url);
24 | res.writeHead(404, {'Content-Type': 'text/html'} );
25 | res.end('404 not found')
26 | }
27 |
28 | });
29 |
30 | app.listen(3000, 'localhost');
31 |
32 | console.log('server app running at localhost:3000');
33 |
34 |
--------------------------------------------------------------------------------
/lesson-03-B/app.js:
--------------------------------------------------------------------------------
1 | // create an express app
2 | var express = require('express'),
3 | morgan = require('morgan'),
4 | app = express(),
5 | port = process.env.PORT || 3000,
6 | testMode = false;
7 |
8 | // add logging middleware to log each request
9 | // see: https://www.npmjs.org/package/morgan
10 | app.use(morgan('dev'));
11 |
12 | // add middleware to always send a 'hello world' response
13 | app.use(function(req, res, next) {
14 | if (req.url == '/test') {
15 | console.log('enabling test mode');
16 | testMode = true;
17 | }
18 |
19 | next();
20 | });
21 |
22 | // add middleware to always send a 'hello world' response
23 | app.use(function(req, res) {
24 | var data = testMode
25 | ? JSON.stringify(req.headers)
26 | : 'hello world
';
27 |
28 | var contentType = testMode ? 'text/plain' : 'text/html';
29 |
30 | res.writeHead(200, {'Content-Type': contentType });
31 | res.end(data);
32 | });
33 |
34 | app.listen(port);
35 |
36 | console.log('server started on port %s', port);
37 |
38 |
--------------------------------------------------------------------------------
/lesson-08/app.js:
--------------------------------------------------------------------------------
1 | var app = require('express')(),
2 | port = process.env.PORT || 3000;
3 |
4 | app.get('/hello1', function(req, res) {
5 | res.send('hello world
'); // automatic -> text/html
6 | });
7 |
8 | app.get('/hello2', function(req, res) {
9 | res.header('Content-Type', 'text/plain');
10 | res.send('hello world\n'); // explicit -> text/plain
11 | });
12 |
13 | app.get('/hello3', function(req, res) {
14 | res.send(new Buffer('hello world\n')); // automatic -> application/octet-stream
15 | });
16 |
17 | app.get('/hello4', function(req, res) {
18 | res.send({message: 'hello world'}); // automatic -> application/json
19 | });
20 |
21 | app.get('/hello5', function(req, res) {
22 | res.send(['hello world']); // automatic -> application/json
23 | });
24 |
25 | app.get('/hello6', function(req, res) {
26 | res.header('Content-Type', 'text/xml');
27 | res.send('hello world') // explicit -> text/xml;
28 | });
29 |
30 | app.listen(port);
31 |
--------------------------------------------------------------------------------
/lesson-10/app.js:
--------------------------------------------------------------------------------
1 | var app = require('express')(),
2 | bodyParser = require('body-parser'),
3 | morgan = require('morgan'),
4 | port = process.env.PORT || 3000,
5 | MongoClient = require('mongodb').MongoClient,
6 | mongoUrl = 'mongodb://localhost:27017/expressdemo',
7 | _db;
8 |
9 | app.use(morgan('dev'));
10 |
11 | app.use(bodyParser.json());
12 |
13 | MongoClient.connect(mongoUrl, function(err, db) {
14 | if (err) {
15 | console.error(err);
16 | } else {
17 | console.log('connected to mongo');
18 | _db = db;
19 | app.listen(port, function() {
20 | console.log('listening for requests on localhost:%s', port);
21 | });
22 | }
23 | });
24 |
25 | app.get('/users', function(req, res) {
26 | var collection = _db.collection('data');
27 | collection.find({}).toArray(function(err, result) {
28 | if (err) {
29 | console.error(err);
30 | res.status(500).end();
31 | } else {
32 | res.send({ success: true, users: result });
33 | }
34 | });
35 | });
36 |
37 |
38 | app.get('/users/:name', function(req, res) {
39 | var name = req.params.name;
40 | var collection = _db.collection('data');
41 |
42 | collection.findOne({ name: name }, function(err, user) {
43 | if (err) {
44 | console.error(err);
45 | res.status(500).end();
46 | } else {
47 | var result = user
48 | ? { success: true, user: user }
49 | : { success: false, reason: 'user not found: ' + name };
50 | res.send(result);
51 | }
52 | });
53 | });
54 |
55 |
56 | app.post('/users', function(req, res) {
57 | var user = req.body;
58 | var collection = _db.collection('data');
59 |
60 | // validate user, make sure doesn't already exist, etc.
61 |
62 | collection.insert(user, function(err, users) {
63 | if (err) {
64 | console.error(err);
65 | res.status(500).end();
66 | } else {
67 | res.send({ success: true, user: user })
68 | }
69 | });
70 | });
71 |
72 |
73 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | express-demo
2 | ============
3 |
4 | Tony Pujals
5 | @subfuzion
6 |
7 | MIT License
8 |
9 |
10 | ### lesson-01
11 | Simple node http server
12 |
13 |
14 | ### lesson-02
15 | Simple node http server that reads index.html from file system
16 |
17 | Building a full-fledged web framework is a lot of work:
18 |
19 | * should support multiple formats and mime types (example only returns text/html)
20 | * need to be more flexible about loading resources (example is hard-coded for index.html)
21 | * should stream the response data (servers shouldn't buffer files into memory)
22 | * doesn't parse url for query parameters
23 | * doesn't do any caching, and doesn't set any client cache headers
24 |
25 |
26 | ### lesson-03-A
27 | Simple Express "Hello World" example using middleware
28 |
29 |
30 | ### lesson-03-B
31 | Simple Express example with multiple middlewares
32 |
33 |
34 | ### lesson-03-C
35 | Simple Express example demonstrating express static middleware
36 |
37 |
38 | ### lesson-04
39 | Simple Express "Hello World" example
40 |
41 |
42 | ### lesson-05
43 | Simple Express "Hello World" example using listen callback
44 |
45 |
46 | ### lesson-06
47 | Simple Express "Hello World" example using listen callback and demonstrating listen return object
48 |
49 |
50 | ### lesson-07
51 | Simple Express routing example
52 |
53 | ### lesson-08
54 |
55 | Simple Express routing example that returns different content type responses.
56 |
57 | ### lesson-09
58 |
59 | Simple Express example for serving an API.
60 |
61 | ### lesson-10
62 |
63 | Simple Express example using Mongo
64 |
65 |
66 | ## Notes
67 |
68 | ### Useful Links
69 |
70 | #### Express
71 | * http://expressjs.com/
72 |
73 | #### Node Docs
74 | * http://nodejs.org/api/http.html
75 | * http://nodejs.org/api/net.html
76 | * http://nodejs.org/api/path.html
77 |
78 |
79 | ### Common Middleware
80 |
81 | #### body-parser
82 |
83 | * https://github.com/expressjs/body-parser
84 | * handles urlencoded and json bodies
85 | * for multipart bodies / file uploads try connect-busboy
86 |
87 | #### cookie-parser
88 | * https://github.com/expressjs/cookie-parser
89 |
90 | #### static-favicon
91 | * now use serve-favicon
92 | * https://github.com/expressjs/serve-favicon
93 |
94 | #### morgan (automatic request/response logging)
95 | * https://github.com/expressjs/morgan)
96 |
97 | #### More: https://github.com/senchalabs/connect
98 |
99 |
--------------------------------------------------------------------------------
/lesson-09/app.js:
--------------------------------------------------------------------------------
1 | var app = require('express')(),
2 | bodyParser = require('body-parser'),
3 | morgan = require('morgan'),
4 | port = process.env.PORT || 3000,
5 | _ = require('underscore'),
6 | users = [
7 | { id: 1, name: 'tony' }
8 | ];
9 |
10 | app.use(morgan('dev'));
11 |
12 | app.use(bodyParser.json());
13 |
14 | app.get('/users', function(req, res) {
15 | res.send({ success: true, users: users });
16 | });
17 |
18 |
19 | app.get('/users/:name', function(req, res) {
20 | var name = req.params.name;
21 |
22 | var user = _.find(users, function(u) {
23 | return u.name == name;
24 | });
25 |
26 | var result = user
27 | ? { success: true, user: user }
28 | : { success: false, reason: 'user not found: ' + name };
29 |
30 | res.send(result);
31 | });
32 |
33 |
34 | app.post('/users', function(req, res) {
35 | var user = req.body;
36 |
37 | console.log(user);
38 |
39 | if (!user || !user.name) {
40 | res.send({ success: false, reason: 'cannot create user (missing user name)' });
41 | return;
42 | }
43 |
44 | var existing = _.findWhere(users, { name: user.name });
45 |
46 | if (existing) {
47 | res.send({ success: false, reason: 'user already exists: ' + existing.name });
48 | return;
49 | }
50 |
51 | users.push(user);
52 | user.id = users.length;
53 |
54 | res.send({ success: true, user: user })
55 |
56 | });
57 |
58 | app.put('/users/:name', function(req, res) {
59 | var name = req.params.name,
60 | newName = req.body.name;
61 |
62 | var user = _.find(users, function(u) {
63 | return u.name == name;
64 | });
65 |
66 | if (user) {
67 | user.name = newName;
68 | }
69 |
70 | var result = user
71 | ? { success: true, user: user }
72 | : { success: false, reason: 'user not found: ' + name };
73 |
74 | res.send(result);
75 | });
76 |
77 |
78 | app.delete('/users/:name', function(req, res) {
79 | var name = req.params.name;
80 |
81 | var user = _.find(users, function(u) {
82 | return u.name == name;
83 | });
84 |
85 | var result = user
86 | ? { success: true, user: user }
87 | : { success: false, reason: 'user not found: ' + name };
88 |
89 |
90 | users = _.reject(users, function(u) {
91 | return u.name == name;
92 | });
93 |
94 | res.send(result);
95 | });
96 |
97 |
98 | app.listen(port);
99 |
100 |
101 |
--------------------------------------------------------------------------------